问题
Let's say I have this string:
$myString = 'aaa = b AND cc = dd OR e = ff_%_g AND hh = iii AND j = k_%_lll ...'
I want to check if the substring '_%_'
is present, and in that case, I want to replace the previous (and only the previous) '='
by a 'LIKE'
.
Namely, I want this:
$myString = 'aaa = b AND cc = dd OR e LIKE ff_%_g AND hh = iii AND j LIKE k_%_lll ...'
I know how to replace all the '='
using strpos()
and substr_replace()
, but I need to replace only those that precede a '_%_'
Any help, please?
回答1:
Here you go:
$myString = preg_replace("/=([^=]*_%_[^ ]*)/", "LIKE $1", $myString);
来源:https://stackoverflow.com/questions/22327199/php-replace-preceding-substrings-of-some-other-substring