【问题标题】:php strip off the first 10 numbers from a string and put the remaining in a new string [closed]php从字符串中删除前10个数字并将剩余的数字放入一个新字符串中[关闭]
【发布时间】:2012-12-26 20:33:05
【问题描述】:

我想从字符串中删除前 10 个数字,然后将原始字符串中的剩余数字变成一个新字符串以供使用。

例子:

$org_string = '304928340912';

我认为它是通过正则表达式或 trim() 函数完成的,但对它们了解不够,无法定义从一开始的第 10 个数字的修剪位置。

我正在寻找的结果将是(基于上面的 org_string):

$new_string = '12';

最后的数字可能是 1、2、3、4 位数字,所以我可以去掉最后 x 个数字。

关于如何做到这一点的任何想法?

【问题讨论】:

  • 使用$new_string = substr($org_string, 10);

标签: php string trim


【解决方案1】:

使用substr

$new_string = substr($org_string, 10);

【讨论】:

  • substr($org_string, 0, 10) 返回3049283409 不是12
  • @Baba 你是对的!抱歉,已修复。
  • 这将返回前十个字符,substr($org_string, 10) 就是您要查找的内容。
  • 我已经编辑了我的帖子。
  • 你试过上面的更新代码了吗?
【解决方案2】:
$new_string = '';
if (strlen($org_string) > 10)
{
    $new_string = substr($org_string, 10);
}
echo $new_string;

否则我们可以获得false

substr (PHP 4, PHP 5) 返回字符串的一部分

返回: 类型:字符串 描述:字符串的提取部分; 或 FALSE 失败,或空字符串。

【讨论】:

    猜你喜欢
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 2011-11-03
    • 1970-01-01
    • 2011-04-20
    • 1970-01-01
    • 2012-09-12
    相关资源
    最近更新 更多