PHP:如何在所有HTML标记中将单引号转换为双引号?

时间:2022-09-15 16:05:49

How can I convert all single quotes to double quotes in all HTML tags only? Is there an easier way to do it? Thanks :)

如何才能将所有单引号转换为所有HTML标记中的双引号?有更简单的方法吗?谢谢 :)

For example: How can I convert this string (actual data from my work):

例如:如何转换此字符串(来自我的工作的实际数据):

<TEXTFORMAT LEADING='2'><P ALIGN='LEFT'><FONT FACE='Verdana' style="font-size:10' COLOR='#0B333C'>My name's Mark</FONT></P></TEXTFORMAT>

To this:

<TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Verdana" style="font-size:10" COLOR="#0B333C">My name's Mark</FONT></P></TEXTFORMAT>

6 个解决方案

#1


If you don't care about the JavaScript and CSS issues mentioned elsewhere, try this:

如果您不关心其他地方提到的JavaScript和CSS问题,请尝试以下方法:

$text = "<TEXTFORMAT LEADING='2'><P ALIGN='LEFT'><FONT FACE='Verdana' style='font-size:10' COLOR='#0B333C'>My name's Mark</FONT></P></TEXTFORMAT>";
echo preg_replace('/<([^<>]+)>/e', '"<" . str_replace("\\\\\'", \'"\', "$1") . ">"', $text);

This is taken from a thread by someone with exactly the same problem as you over at devshed.com.

这是来自devshed.com上与您完全相同问题的某个人的线程。

#2


I'm assuming that when you say in all html tags, that you mean all single quotes that contain an attribute. You wouldn't want <a onclick="alert('hi')"> converted b/c it would break the code.

我假设当你在所有html标签中说,你的意思是所有包含属性的单引号。你不希望转换为b / c它会破坏代码。

Any regular expression is going to be fragile. If you know your input will be a particular set of simple cases, you might be ok with a regex. Otherwise, you'll want a DOM parser that understands complex html markup like onmouseover="(function () { document.getElementById(''); alert(\"...\")...})()" (for example). Add to that an attribute can span multiple lines. ;)

任何正则表达式都将是脆弱的。如果您知道您的输入将是一组特定的简单案例,那么您可以使用正则表达式。否则,你需要一个理解复杂html标记的DOM解析器,如onmouseover =“(function(){document.getElementById(''); alert(\”... \“)...})()”(例如)。除此之外,属性可以跨越多行。 ;)

I haven't had to tackle this particular problem recently, but maybe there's a good way to do it with HTML Tidy (more here: http://devzone.zend.com/article/761) or a parser like this one http://sourceforge.net/projects/simplehtmldom/

我最近没有必要解决这个特殊的问题,但也许有一个很好的方法来使用HTML Tidy(更多这里:http://devzone.zend.com/article/761)或像这样的解析器http: //sourceforge.net/projects/simplehtmldom/

#3


I know i could hav'e using regex, but give this a try: assign $string the contents using fpen(), fread() etc...

我知道我可以使用正则表达式,但尝试一下:使用fpen(),fread()等分配$ string内容...

$string = str_replace("'", '"', $string);
$array = explode('>', $string);
foreach($array as $key => $value){
    if(strpos($value, '<') <> 0 ){
       $array[$key] = str_replace('"', "'",$value);
    }
}
$string = implode('>',$array);

#4


Not really sure exactly what you are trying to accomplish... Replacing pieces of the string using php can be done using the str_replace function:

不确定你要完成什么...使用php替换字符串片段可以使用str_replace函数完成:

str_replace("'", "\"", $yourString);

#5


Use Tidy which can fix your HTML soup and output clean XHTML. It does other nice things too, like fixing nesting problems, lowercasing tags, etcetera, etcetera.

使用Tidy可以修复你的HTML汤并输出干净的XHTML。它还做了其他很好的事情,比如修复嵌套问题,降低标记,等等等。

#6


I would go with either a dom parser or roll my own simple tag parser that understands quoting as well as escaping quote characters so that it doesn't take "he said \"blah\"" as he said \, blah\ and empty string.

我会选择一个dom解析器或者滚动我自己的简单标签解析器,它能理解引用以及转义引号字符,这样它就不会像他说的那样“bla bla bla as as as as,,,,,,,\ \ empty empty empty 。

It could detect whether the quoting to be modified is inside a tag easily. Over many years I have learned that regular expressions are way too fragile for such tasks.

它可以检测要修改的引用是否容易在标记内。多年来,我了解到正则表达式对于此类任务来说太脆弱了。

#1


If you don't care about the JavaScript and CSS issues mentioned elsewhere, try this:

如果您不关心其他地方提到的JavaScript和CSS问题,请尝试以下方法:

$text = "<TEXTFORMAT LEADING='2'><P ALIGN='LEFT'><FONT FACE='Verdana' style='font-size:10' COLOR='#0B333C'>My name's Mark</FONT></P></TEXTFORMAT>";
echo preg_replace('/<([^<>]+)>/e', '"<" . str_replace("\\\\\'", \'"\', "$1") . ">"', $text);

This is taken from a thread by someone with exactly the same problem as you over at devshed.com.

这是来自devshed.com上与您完全相同问题的某个人的线程。

#2


I'm assuming that when you say in all html tags, that you mean all single quotes that contain an attribute. You wouldn't want <a onclick="alert('hi')"> converted b/c it would break the code.

我假设当你在所有html标签中说,你的意思是所有包含属性的单引号。你不希望转换为b / c它会破坏代码。

Any regular expression is going to be fragile. If you know your input will be a particular set of simple cases, you might be ok with a regex. Otherwise, you'll want a DOM parser that understands complex html markup like onmouseover="(function () { document.getElementById(''); alert(\"...\")...})()" (for example). Add to that an attribute can span multiple lines. ;)

任何正则表达式都将是脆弱的。如果您知道您的输入将是一组特定的简单案例,那么您可以使用正则表达式。否则,你需要一个理解复杂html标记的DOM解析器,如onmouseover =“(function(){document.getElementById(''); alert(\”... \“)...})()”(例如)。除此之外,属性可以跨越多行。 ;)

I haven't had to tackle this particular problem recently, but maybe there's a good way to do it with HTML Tidy (more here: http://devzone.zend.com/article/761) or a parser like this one http://sourceforge.net/projects/simplehtmldom/

我最近没有必要解决这个特殊的问题,但也许有一个很好的方法来使用HTML Tidy(更多这里:http://devzone.zend.com/article/761)或像这样的解析器http: //sourceforge.net/projects/simplehtmldom/

#3


I know i could hav'e using regex, but give this a try: assign $string the contents using fpen(), fread() etc...

我知道我可以使用正则表达式,但尝试一下:使用fpen(),fread()等分配$ string内容...

$string = str_replace("'", '"', $string);
$array = explode('>', $string);
foreach($array as $key => $value){
    if(strpos($value, '<') <> 0 ){
       $array[$key] = str_replace('"', "'",$value);
    }
}
$string = implode('>',$array);

#4


Not really sure exactly what you are trying to accomplish... Replacing pieces of the string using php can be done using the str_replace function:

不确定你要完成什么...使用php替换字符串片段可以使用str_replace函数完成:

str_replace("'", "\"", $yourString);

#5


Use Tidy which can fix your HTML soup and output clean XHTML. It does other nice things too, like fixing nesting problems, lowercasing tags, etcetera, etcetera.

使用Tidy可以修复你的HTML汤并输出干净的XHTML。它还做了其他很好的事情,比如修复嵌套问题,降低标记,等等等。

#6


I would go with either a dom parser or roll my own simple tag parser that understands quoting as well as escaping quote characters so that it doesn't take "he said \"blah\"" as he said \, blah\ and empty string.

我会选择一个dom解析器或者滚动我自己的简单标签解析器,它能理解引用以及转义引号字符,这样它就不会像他说的那样“bla bla bla as as as as,,,,,,,\ \ empty empty empty 。

It could detect whether the quoting to be modified is inside a tag easily. Over many years I have learned that regular expressions are way too fragile for such tasks.

它可以检测要修改的引用是否容易在标记内。多年来,我了解到正则表达式对于此类任务来说太脆弱了。