【发布时间】:2016-12-01 12:52:50
【问题描述】:
我正在尝试使用 preg_replace 或 str_ireplace 将 span 类标签包裹在找到的关键字周围,但是使用 str_ireplace 会删除诸如“wooding”之类的词一半例如:
<span class="highlight">wood</span>ing
下面是针、干草堆和要求返回的示例:
针:
wood
干草堆:
wood and stuff
this doesnt contain the keyword
Wooding is what we do
我想退货:
<span class="highlight">wood</span> and stuff
this doesnt contain the keyword
<span class="highlight">Wooding</span> is what we do
这是我的 preg_replace 实验的链接: http://www.phpliveregex.com/p/i4m
【问题讨论】:
-
你的文本已经是 html 文档了吗?
-
没有 PHP 字符串(去除 HTML)
-
你的意思是它还没有包含任何html标签?
-
还有多少关键字需要替换?
-
匹配单词第一部分的任何内容。所以返回突出显示的词可能是树林、木教堂、林地等
标签: php regex preg-replace str-replace