替换引号内的字符串,避免使用标签属性

问题描述 投票:-1回答:1

我只需要在引号内的字符串之间替换文本,我想忽略标记属性内的文本。我想为替换添加标签。

我有:

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;

感谢您的大力支持。

php regex preg-replace
1个回答
0
投票

我找到了解决方案:

$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] *)” /

© www.soinside.com 2019 - 2024. All rights reserved.