我有像
这样的PHP字符串$str1 = "hello ... this is the rest of the line"
或
$str1 = "ASDFDF ... this is also the rest of the line";
我正在尝试修正一个正则表达式语句,该语句将在字符串中出现“...”后提取文本。我无法可靠地做到这一点..
所以在上述情况下,我想......
$extract = "this is the rest of the line";
...你明白了。
答案 0 :(得分:3)
为什么要使用正则表达式?只需爆炸字符串并拾取结果中的第二个元素:
$str = "hello ... this is the rest of the line";
list(, $rest) = explode(" ... ", $str, 2) + array(, '');
这基本上是一回事,而且这个正则表达式并不快。
答案 1 :(得分:2)
有多种方法可以做到。
使用strpos和substr:
function rest_of_line($line){
$loc = strpos($line, '...');
if($loc !== FALSE){
return substr($line, $loc+3);
}
return $line;
}
$str1 = "hello ... this is the rest of the line";
$str2 = "ASDFDF ... this is also the rest of the line";
echo rest_of_line($str1);
echo rest_of_line($str2);
或使用爆炸:
$rest = explode('...', $str1, 2); // the 2 ensures that only the first occurrence of ... actually matters.
echo $rest[1]; // you should probably check whether there actually was a match or not
答案 2 :(得分:0)
explode
它位于...
这是一个很棒的功能:)