$string = 'test check one two test3';
$result = mb_eregi_replace ( 'test|test2|test3' , '<$1>' ,$string ,'i');
echo $result;
This should deliver: <test> check one two <test3>
Is it possible to get, that test and test3 was found, without using another match function ?
You can use
preg_replace_callbackinstead:Here
preg_replace_callbackwill call the passed callback function for each match of the pattern (note that its syntax differs from POSIX). In this case the callback function is an anonymous function that adds the match to the$matchesarray and returns the substitution string that the matches are to be replaced by.Another approach would be to use
preg_splitto split the string at the matched delimiters while also capturing the delimiters:The result is an array of alternating non-matching and matching parts.