我需要从字符串中获取子字符串(请参阅示例,粗体部分)。所有字符串都以"输入"开头。接着是2个下划线,其间有一些(1到7个)随机字符。谢谢!
示例:
input_7ax8_的 SOME_INFO
input_3f0max2_的 SOME_OTHER_INFO
input_k_的 ANOTHERINFO-任何-字符-可能:0123456789
答案 0 :(得分:2)
你只需要explode
及其第三个参数:
<?php
$input = 'input_7ax8_SOME_INFO';
$input = explode("_",$input,2); // Split 2 times
$input[2] = '<b>'.$input[2].'</b>'; // Make the rest of the string bold
$input = implode("_",$input); // re joining
echo $input;
?>
答案 1 :(得分:2)
使用&#34;非下划线&#34; +&#34;下划线&#34;时间2并取出之后的所有内容,您可以得到您要求的结果。
?:用于不返回带下划线的部件的结果,因为需要()将它们组合在一起。
$input = 'input_k_ANOTHERINFO-any-chars-possible:0123456789';
preg_match( '~^(?:[^_]+_){2}(.*)$~', $input, $match );
var_export($match);