替换所有不在html标签中的引号 [英] Replace all quotes that are not in html-tags
问题描述
目前,我正在用特殊引号替换文本中的所有引号。
但是我怎样才能改变我的正则表达式,只有文本内部的引号会被替换,而不是那些在html标签中使用的正则表达式。
$ text = preg_replace('/(?= \ w)/',& ra;;,$ text);
$ text = preg_replace('/(?<= \w)/',& laquo;,$ text);
我不适合在正则表达式中使用。问题是我需要使用另一个符号替换起始引号而不是结束引号。
如果您确实需要更多信息,请这么说。
任何帮助都会被赞赏!
编辑
测试用例
< p>这是一个精彩的长文本。至少应该是。这里我们有一个< a href =http://wwww.site-to-nowhere.comtarget =_ blank>链接< / a>。< / p>
预期的输出应该是:
< p>这是& amp; raquo;美妙的长文字& laquo ;.至少应该是。这里我们有一个< a href =http://wwww.site-to-nowhere.comtarget =_ blank>链接< / a>。< / p>
现在就是这样:
< p>这是& amp; raquo;美妙的长文字& laquo ;.至少应该是。这里我们有一个< a href =& raquo; http://wwww.site-to-nowhere.com& laquo;目标=安培; RAQUO; _blank&安培; LAQUO;> LINK< / A>。< / p为H.
编辑2
Thx为Kamehameha的答案,我已将以下代码添加到脚本中:
$ text = preg_replace(/ \ (文本);(文本);([^'] *?)\(?= [^>] +?<)/,& raquo; \ 1& code>
在正则表达式测试中,什么工作都不会取代任何东西。我做错了什么?
这个正则表达式适用于给定的字符串。
搜索 - ([^>] *?)(?= [^>] *?<)
用 - & ; RAQUO; \1&安培; LAQUO;
演示这里
测试它 -
输入 -
< ; p>这是一个精彩的长文本。 另一个精彩的文字至少应该是。这里我们有一个< a href =http://wwww.site-to-nowhere.comtarget =_ blank>链接< / a>。< / p>
输出 -
这是& amp; raquo;美妙的长文字& laquo ;. & raquo;另一个精彩的文字& laquo;至少应该是。这里我们有一个< a href =http://wwww.site-to-nowhere.comtarget =_ blank>链接< / a>。< / p>
编辑1 -
执行此操作PHP -
$ str ='< p>这是一个美妙的长文本。 另一个精彩的文字至少应该是。这里我们有一个< a href =http://wwww.site-to-nowhere.comtarget =_ blank>链接< / a>。< / p>';
var_dump(preg_replace('/([^'] *?)(?= [^>] *?<)/','& raquo; \\'1& laquo' ,$ str));
输出 -
/ **输出** /
字符串'< p>这是& amp; raquo;美妙的长文字& laquo。 & raquo;另一个精彩的文字& laquo至少应该是。这里我们有< a href =http://wwww.site-to-nowhere.comtarget =_ blank>链接< / a>。< / p>'(长度= 196)
编辑2 -
您已执行 preg_replace
功能正常,但在替换字符串中,您在双引号()中使用了\ 1。这样做,你逃离了1本身,并且不会被替换。
为了更清楚,试试看看会发生什么 -
echo'& ra;; \1& laquo;';
echo& ra;; \1& laquo;;
第二个\1应该不可见。 阅读这个页面以获得更多的清晰度。 编辑3 - 演示此处 currently i am replacing all my quotes inside a text with special quotes.
But how can i change my regex that only quotes inside the text will be replaced and not the ones who are used in html tags. I am not that fit in regular expressions. The problem is that i need to replace the starting quotes with another symbol than ending quotes. If you do need more information, say so. Any help is appreciated! EDIT Test Case The expected output should be: Right now it is like this: EDIT 2 Thx for the answer of Kamehameha i've added the following code to my script: What worked great in the regex tester does not replace anything. Did i do anything wrong? This regex works for the given strings. Demo here EDIT 1- It's output - EDIT 2- The second \1 should not be visible. Read the Replacement section in this page for more clarity. EDIT 3- Demo here 这篇关于替换所有不在html标签中的引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
所以解决方案将会是其中之一 -
pre $ preg_replace('/([^<>] *?)(?= [^>] *?<)/','& ra;; \1& laquo;',$ str)
preg_replace(/ \([^<> )\(?= [^>] *?<)/,& raquo; \\1& laquo;,$ str)
preg_replace(/ \( (?= [^>]<<)/,& raquo; $ 1& laquo;,$ str)
< code>
覆盖文本的正则表达式可能不包含在标签中 -
\([^<>] *?)\(? =(?:[^>] *?(?:<| $))
$text = preg_replace('/"(?=\w)/', "»", $text);
$text = preg_replace('/(?<=\w)"/', "«", $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 »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 »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>
$text = preg_replace("/\"([^<>]*?)\"(?=[^>]+?<)/", "»\1«", $text);
Search for - "([^<>]*?)"(?=[^>]*?<)
Replace with - »\1«
Testing it - 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 »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>
Executing this in 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('/"([^<>]*?)"(?=[^>]*?<)/', '»\1«', $str));
/** OUTPUT **/
string '<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>' (length=196)
You have executed the preg_replace
function properly, but in the replacement string, you have used \1 inside the Double quotes(""). Doing so, you are escaping the 1 itself and that won't be replaced.
To make it more clear, try this and see what happens -echo '»\1«';
echo "»\1«";
So the solution would be one of these -preg_replace('/"([^<>]*?)"(?=[^>]*?<)/', '»\1«', $str)
preg_replace("/\"([^<>]*?)\"(?=[^>]*?<)/", "»\\1«", $str)
preg_replace("/\"([^<>]*?)\"(?=[^>]*?<)/", "»$1«", $str)
A regex that covers text which might not be enclosed within tags- \"([^<>]*?)\"(?=(?:[^>]*?(?:<|$)))