为了账号安全,请及时绑定邮箱和手机立即绑定

找到数字后如何获取字符串的剩余部分

找到数字后如何获取字符串的剩余部分

PHP
芜湖不芜 2022-10-28 15:28:27
找到数字后如何获取字符串的剩余部分。例 1) hello12cool4here输出应该是:2cool4here例 2) hel3lo12cool4here输出应该是:lo12cool4here
查看完整描述

2 回答

?
慕工程0101907

TA贡献1887条经验 获得超5个赞

使用正则表达式:


preg_match('/^\D*\d(.*)/', 'hello12cool4here', $matches);

echo($matches[1]);    // 2cool4here

preg_match('/^\D*\d(.*)/', 'hel3lo12cool4here', $matches);

echo($matches[1]);    // lo12cool4here

preg_match('/^\D*\d(.*)/', '42lo12cool4here', $matches);

echo($matches[1]);    // 2lo12cool4here


查看完整回答
反对 回复 2022-10-28
?
慕仙森

TA贡献1827条经验 获得超7个赞

您可以遍历所有字符并测试它们是否为数字,找到第一个时返回:


$str='hello12cool4here';


for( $i=0; $i< strlen( $str ); $i++ ){

    if( is_numeric( substr($str,$i,1) ) )exit( substr( $str, $i+1 ) );

}

或将其放入函数中:


function findremainder( $str ){

    for( $i=0; $i< strlen( $str ); $i++ ){

        if( is_numeric( substr($str,$i,1) ) )return( substr( $str, $i+1 ) );

    }

    return $str;

}



echo findremainder( $str );


查看完整回答
反对 回复 2022-10-28
  • 2 回答
  • 0 关注
  • 64 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信