PHP replace preceding substrings of some other substring

假装没事ソ 提交于 2020-01-04 09:37:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!