替换所有不在html标签中的引号 [英] Replace all quotes that are not in html-tags

查看:123
本文介绍了替换所有不在html标签中的引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在用特殊引号替换文本中的所有引号。
但是我怎样才能改变我的正则表达式,只有文本内部的引号会被替换,而不是那些在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应该不可见。

所以解决方案将会是其中之一 -

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

阅读这个页面以获得更多的清晰度。



编辑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.

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

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

<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>

The expected output should be:

<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>

Right now it is like this:

<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>

EDIT 2

Thx for the answer of Kamehameha i've added the following code to my script:

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

What worked great in the regex tester does not replace anything. Did i do anything wrong?

解决方案

This regex works for the given strings.

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

Demo here
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 &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>

EDIT 1-
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('/"([^<>]*?)"(?=[^>]*?<)/', '&raquo;\1&laquo', $str));

It's output -

/** 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)

EDIT 2-
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 '&raquo;\1&laquo;';
echo "&raquo;\1&laquo;";

The second \1 should not be visible.
So the solution would be one of these -

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

Read the Replacement section in this page for more clarity.

EDIT 3-
A regex that covers text which might not be enclosed within tags-

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

Demo here

这篇关于替换所有不在html标签中的引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆