【问题标题】:Replace all quotes that are not in html-tags替换所有不在 html-tags 中的引号
【发布时间】:2023-04-02 09:35:01
【问题描述】:

目前我正在用特殊引号替换文本中的所有引号。 但是我怎样才能改变我的正则表达式,只替换文本内的引号,而不是那些在 html 标签中使用的。

$text = preg_replace('/"(?=\w)/', "»", $text);
$text = preg_replace('/(?<=\w)"/', "&laquo;", $text);

我不太适合正则表达式。问题是我需要用另一个符号而不是结束引号替换起始引号。

如果您确实需要更多信息,请说出来。

感谢任何帮助!

编辑

测试用例

<p>This is a "wonderful long text". At least it should be. Here we have a <a href="http://wwww.site-to-nowhere.com" target="_blank">link</a>.</p>

预期的输出应该是:

<p>This is a &raquo;wonderful long text&laquo;. At least it should be. Here we have a <a href="http://wwww.site-to-nowhere.com" target="_blank">link</a>.</p>

现在是这样的:

<p>This is a &raquo;wonderful long text&laquo;. At least it should be. Here we have a <a href=&raquo;http://wwww.site-to-nowhere.com&laquo; target=&raquo;_blank&laquo;>link</a>.</p>

编辑 2

感谢 Kamehameha 的回答,我在脚本中添加了以下代码:

$text = preg_replace("/\"([^&lt;&gt;]*?)\"(?=[^&gt;]+?&lt;)/", "&amp;raquo;\1&amp;laquo;", $text);

在正则表达式测试器中效果很好并不能替代任何东西。我是不是做错了什么?

【问题讨论】:

  • 一些测试用例以及输出应该如何?
  • 安全、缓慢且明显的方法:放入 HTML/XML 解析库并操作文本节点?
  • @AmitJoki:我添加了一个例子!
  • @TobiasKun,看看我的回答
  • @UlrichSchwarz:谢谢输入。我自己也想过,但真心希望有更好的正则表达式方法!

标签: php html regex replace


【解决方案1】:

此正则表达式适用于给定的字符串。

Search for   - "([^<>]*?)"(?=[^>]*?<)
Replace with - &raquo;\1&laquo;

演示here
测试它 -

INPUT - 
<p>This is a "wonderful long text". "Another wonderful ong text" At least it should be. Here we have a <a href="http://wwww.site-to-nowhere.com" target="_blank">link</a>.</p>

OUTPUT - 
<p>This is a &raquo;wonderful long text&laquo;. &raquo;Another wonderful ong text&laquo; At least it should be. Here we have a <a href="http://wwww.site-to-nowhere.com" target="_blank">link</a>.</p>

编辑 1-
在 PHP 中执行 -

$str = '<p>This is a "wonderful long text". "Another wonderful ong text" At least it should be. Here we have a <a href="http://wwww.site-to-nowhere.com" target="_blank">link</a>.</p>';
var_dump(preg_replace('/"([^<>]*?)"(?=[^>]*?<)/', '&raquo;\1&laquo', $str));

这是输出 -

/** OUTPUT **/
string '<p>This is a &raquo;wonderful long text&laquo. &raquo;Another wonderful ong text&laquo At least it should be. Here we have a <a href="http://wwww.site-to-nowhere.com" target="_blank">link</a>.</p>' (length=196)

编辑 2-
您已经正确执行了preg_replace 函数,但是在替换字符串中,您在双引号("") 中使用了\1。这样做,您将转义 1 本身并且不会被替换。
为了更清楚,试试这个,看看会发生什么 -

echo '&raquo;\1&laquo;';
echo "&raquo;\1&laquo;";

第二个 \1 不应该是可见的。
所以解决方案就是其中之一-

preg_replace('/"([^<>]*?)"(?=[^>]*?<)/', '&raquo;\1&laquo;', $str)
preg_replace("/\"([^<>]*?)\"(?=[^>]*?<)/", "&raquo;\\1&laquo;", $str)
preg_replace("/\"([^<>]*?)\"(?=[^>]*?<)/", "&raquo;$1&laquo;", $str)

阅读this page 中的替换部分以获得更清晰的信息。

编辑 3-
覆盖可能未包含在标签中的文本的正则表达式-

\"([^<>]*?)\"(?=(?:[^>]*?(?:<|$)))

演示here

【讨论】:

  • 您的正则表达式工作得非常好。谢谢。但如果我在 php 中使用它,它不会替换任何东西。我将我的新 php 代码添加到我的问题中。你能看看我做错了什么吗?这会很棒!
  • @TobiasKun 当然给我一分钟。
  • 我不明白为什么它对我不起作用 :( 你能用这里的内容试试吗:pastebin.com/yZrtXJ6n
  • @TobiasKun 您在使用引号时遇到了问题。检查编辑 2
  • @TobiasKun 从 pastebin 中尝试了您的文本。它适用于我提供的解决方案。
【解决方案2】:

也可以使用负前瞻:

(?![^<]*>)"([^"]+)"

替换为:&amp;raquo;\1&amp;laquo;

【讨论】:

  • 这更神奇!
【解决方案3】:

作为记录,有一个简单的 PHP 解决方案没有被提及,它有效地跳过了所有 &lt;a...&lt;/a&gt; 标记。

搜索:&lt;a.*?&lt;\/a&gt;(*SKIP)(*F)|"([^"]*)"

替换:&amp;raquo;\1&amp;laquo;

Demo 中,查看底部的替换。

参考

How to match (or replace) a pattern except in situations s1, s2, s3...

【讨论】:

  • 这看起来很棒。谢谢!
【解决方案4】:

使用这个正则表达式:

(?<=^|>)[^><]+?(?=<|$)

这将匹配非 html 字符串。

然后对结果字符串执行正则表达式

【讨论】:

  • 非常感谢。但我不太确定,我该如何使用它。因为最后我需要替换引号的整个文本(包括 html 标签)。
猜你喜欢
  • 2019-10-24
  • 2013-04-15
  • 1970-01-01
  • 1970-01-01
  • 2021-02-25
  • 2013-09-24
  • 2011-07-18
  • 2012-01-01
  • 1970-01-01
相关资源
最近更新 更多