I need to extract an unknown word that immediately follows a word then a comma.
More preceisely, I want to get the word that comes after post
followed by a comma then a space.
Sample string 1:
admin wrote a new post, FIRE 2 days, 2 hours ago
Sample string 2:
admin posted an update 2 days, 3 hours ago
Only the first sample string qualifies and the word FIRE
should be returned.
Using Regular Expression
$string="admin wrote a new post, FIRE 2 days, 2 hours ago";
$result=preg_split('/post,/',$string);
if(count($result)>1){
$result_split=explode(' ',$result[1]);
print_r($result_split[1]);
}
This one outputs FIRE
Another way
$string="admin wrote a new post, FIRE 2 days, 2 hours ago";
$result=preg_match_all('/(?<=(post,))(\s\w*)/',$string,$matches);
print_r($matches[0]);