我只需要在引号内的字符串之间替换文本,我想忽略标记属性内的文本。我想为替换添加标签。
我有:
This text is in <font style="color:#f00;">red</font>. This text have "quotes and some spaces", more text inside "quotes"
我想要的结果:
This text is in <font style="color:#f00;">red</font>. This text have "<b>quotes and some spaces</b>", more text inside "<b>quotes</b>"
忽略标签属性中的文本很重要。我想避免匹配“ color:#f00;” ->“ color:#f00;”
感谢您的大力支持。
我找到了解决方案:
$str = 'This text is in <font style="color:#f00;">red</font>. This text have "quotes and some spaces", more text inside "quotes"';
echo htmlentities(preg_replace('/"([a-zA-Z\s]*)"/', '<b>$1</b>', $str));
结果:
This text is in <font style="color:#f00;">red</font>. This text have <b>quotes and some spaces</b>, more text inside <b>quotes</b>
仅允许字母和空格的正则表达式可以完成这项工作:/“([[a-zA-Z \ s] *)” /