亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

找到數字后如何獲取字符串的剩余部分

找到數字后如何獲取字符串的剩余部分

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條經驗 獲得超8個贊

您可以遍歷所有字符并測試它們是否為數字,找到第一個時返回:


$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 關注
  • 96 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號