如何用 preg_replace 仅替换字符串的最后一个匹配项?
我必须替换 HTML 文档中字符串的最后一个匹配项(例如单词 foo)。问题是 HTML 文档的结构总是随机的。
我正在尝试使用 preg_replace 来实现这一点,但到目前为止我知道如何仅替换第一个匹配项,而不是最后一个匹配项。
谢谢。
I have to replace the last match of a string (for example the word foo) in HTML document. The problem is that the structure of the HTML document is always random.
I'm trying to accomplish that with preg_replace, but so far I know how to replace only the first match, but not the last one.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
之后使用否定外观
在
(?!...)
输出:Use negative look after
(?!...)
output:
将所有文本与后续模式的最后一次匹配的常见方法是使用贪婪点,
.*
。因此,您可以匹配并捕获最后一个text
之前的文本,并用反向引用 + 新值替换:如果
text
是变量内的某个值,则必须将其视为纯文本,使用preg_quote
来 确保所有特殊字符都被转义正确:请参阅在线 PHP 演示 和正则表达式演示。
此处,
(.*)
匹配任何零个或多个字符并将其捕获到第 1 组中(请注意,s
修饰符使点匹配换行符字符),尽可能多,直到最右边(最后)出现的文本
。如果text
是 Unicode 子字符串,则u
修饰符在 PHP 中会很方便(它启用(*UTF)
PCRE 动词,允许将传入字符串解析为Unicode 代码点序列,而不是字节和(*UCP)
动词,使所有速记字符类都能够识别 Unicode(如果有)。${1}
是一个替换反向引用,它是一个占位符,用于保存捕获到第 1 组中的值,以便在结果字符串中恢复该子字符串。您可以使用$1
,但如果 可能会出现问题code>$text 以数字开头。A common approach to match all text to the last occurrence of the subsequent pattern(s) is using a greedy dot,
.*
. So, you may match and capture the text before the lasttext
and replace with a backreference + the new value:If
text
is some value inside a variable that must be treated as plain text, usepreg_quote
to ensure all special chars are escaped correctly:See the online PHP demo and a regex demo.
Here,
(.*)
matches and captures into Group 1 any zero or more chars (note that thes
modifier makes the dot match line break chars, too), as many as possible, up to the rightmost (last) occurrence oftext
. Iftext
is a Unicode substring, theu
modifier comes handy in PHP (it enables(*UTF)
PCRE verb allowing parsing the incoming string as a sequence of Unicode code points rather than bytes and the(*UCP)
verb that makes all shorthand character classes Unicode aware - if any).The
${1}
is a replacement backreference, a placeholder holding the value captured into Group 1 that lets restore that substring inside the resulting string. You can use$1
, but a problem might arise if the$text
starts with a digit.一个例子
An example
当然,这里给出的公认解决方案是正确的。不过,您可能还想看看这篇文章。我在不需要模式并且字符串不包含所使用的函数无法捕获的字符(即多字节字符)的情况下使用它。我还为 dis/regarding case 添加了一个附加参数。
第一行是:
我必须承认我没有测试性能。但是,我认为 preg_replace() 速度较慢,特别是在大字符串上。
Of course the accepted solution given here is correct. Nevertheless you might also want to have a look at this post. I'm using this where no pattern is needed and the string does not contain characters that could not be captured by the functions used (i.e. multibyte ones). I also put an additional parameter for dis/regarding case.
The first line then is:
I have to admit that I did not test the performance. However, I guess that preg_replace() is slower, specially on large strings.