Reputation: 601
I have PHP strings like
$str1 = "hello ... this is the rest of the line"
or
$str1 = "ASDFDF ... this is also the rest of the line";
I am trying to right a regex statement that will extract the text after "..." appears in the string. I am not able to do this reliably..
so in the above cases, i want to...
$extract = "this is the rest of the line";
... you get the point.
Upvotes: 0
Views: 136
Reputation: 12623
Why use regex? Just explode the string and pick up the second element in the result:
$str = "hello ... this is the rest of the line";
list(, $rest) = explode(" ... ", $str, 2) + array(, '');
It's basically the same thing, and the regex for this is no faster.
Upvotes: 3
Reputation: 9121
There are multiple ways to do it.
Using strpos and 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);
Or using explode:
$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
Upvotes: 2