PHP 替换某些其他子字符串的前面的子字符串 [英] PHP replace preceding substrings of some other substring

查看:39
本文介绍了PHP 替换某些其他子字符串的前面的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个字符串:

$myString = 'aaa = b AND cc = dd OR e = ff_%_g AND hh = iii AND j = k_%_lll ...'

我想检查子字符串 '_%_' 是否存在,在这种情况下,我想替换前一个(并且仅是前一个)'=''LIKE' 组成.

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 ...'

我知道如何使用 strpos()substr_replace() 替换所有 '=',但我只需要替换那些在 '_%_'

I know how to replace all the '=' using strpos() and substr_replace(), but I need to replace only those that precede a '_%_'

有什么帮助吗?

推荐答案

给你:

$myString = preg_replace("/=([^=]*_%_[^ ]*)/", "LIKE $1", $myString);

这篇关于PHP 替换某些其他子字符串的前面的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆