PHP: How to convert a single quote to a double quote in all HTML tags?

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

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 Mark</FONT></P></TEXTFORMAT>

For this:

<TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Verdana" style="font-size:10" COLOR="#0B333C">My name Mark</FONT></P></TEXTFORMAT>
+5
source share
6 answers

If you don't like the JavaScript and CSS issues mentioned elsewhere, try the following:

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

This is taken from the stream by someone with the same problem as you, at devshed.com .

+3
source

, html-, , . , <a onclick="alert('hi')"> b/c, .

. , , , . DOM, html, onmouseover="(function () { document.getElementById(''); alert(\"...\")...})()" (). , .;)

, , , HTML Tidy ( : http://devzone.zend.com/article/761) , http://sourceforge.net/projects/simplehtmldom/

+3

, regex, : $string fpen(), fread() ..

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

, ... php str_replace:

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

Tidy, HTML XHTML. , , .. ..

0

I would go either with a parser, or with my own simple tag parser, which understands quoting as well as escaping quotation marks so that it does not accept "he said \"blah\""both he said \, blah\and an empty string.

It can determine if changing a quote is easy inside a tag. Over the years, I have learned that regular expressions are too fragile for such tasks.

0
source

All Articles