1_0这是字符串
456_1_This_is_other_string_next
1999999_12_Is弦太
怎么得到:这是字符串,This_is_other_string_next,Is-string-too
谢谢
只是一个想法的例子:
echo explode('_', '1_0_This is string', 3)[2];
echo explode('_', '1999999_12_Is-string-too', 3)[2];
您可以尝试使用_
将explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] ) : array
作为分隔符来爆炸原始字符串(请参阅http://php.net/manual/en/function.explode.php)。
您可以计算结果数组中的项目数,并相应地返回一些内容。
示例......
$string = "1_0_This is string";
$parts = explode('_', $string);
$result = "";
if (count($parts) >= 3) {
$result = $parts[2];
}
print $result;
使用带限制的preg_replace
echo preg_replace('/[^_]+_/', '', '1999999_12_Is-string-too', 2);
产量
Is-string-too
正则表达式很简单。匹配任何不是_
(超过一次,贪婪),然后是_
。限制2次更换。
使用Preg Split
print_r(preg_split('/^([^_]+_){2}/', '456_1_This_is_other_string_next', -1, PREG_SPLIT_NO_EMPTY));
产量
Array
(
[0] => This_is_other_string_next
)
基本上是相同的正则表达式,但这个匹配模式发生的前2次。你可以用preg_replace做同样的事情,但那里有点毫无意义。