A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearPHP: Escape Quotes ONLY outside of HTML tags (Regex) - page 1
User InfoPosts
PHP: Escape Quotes ONLY outside of HTML tags (Regex)#1
What regular expression can identify double quotes outside of HTML tags (which already will be validated) to escape them to "?

posted date: 2009-04-16 22:01:00


Re: PHP: Escape Quotes ONLY outside of HTML tags (Regex)#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-16 22:01:01


Re: PHP: Escape Quotes ONLY outside of HTML tags (Regex)#3
would this work? \"(?!\s*\w*>)

posted date: 2009-04-16 22:26:00


Re: PHP: Escape Quotes ONLY outside of HTML tags (Regex)#4
There is no such regular expression.<p> <!-- <a href=" --> is this outside " a tag <!-- "> foo </a> --> or not?</p>If you want to do this, you(ll unfortunately have to use an HTML parser. Since you have already validated the HTML, you probably already have a parser to use.

posted date: 2009-04-16 22:33:00


Re: PHP: Escape Quotes ONLY outside of HTML tags (Regex)#5
Don(t use regex for this, use (or write) a parser. The following code assumes that the input HTML string is well formed (as you stated). Be warned that the code will break if it encounters invalid input!If you can(t be sure of the well-formedness, you can give PHP Tidy a try.<?php$html = (<tag>text "text"<tag attr="value"><!-- "text" --> text</tag> "text".(;echo html_escape_quotes($html);/* Parses input HTML and escapes any literal double quotes in the text content with &quot;. Leaves comments alone. */function html_escape_quotes($html){ $output = ""; $length = strlen($html); $delim = "<"; $offset = 0; while ($offset < $length) { $tokpos = strpos($html, $delim, $offset); if ($tokpos === false) $tokpos = $length; $token = substr($html, $offset, $tokpos - $offset); $offset = $tokpos; if ($delim == "<") { $token = str_replace(("(, (&quot;(, $token); $delim = substr($html, $offset, 4) == "<!--" ? "-->" : ">"; } else { $delim = "<"; } $output .= $token; } return $output;}?>

posted date: 2009-04-16 22:46:00


Re: PHP: Escape Quotes ONLY outside of HTML tags (Regex)#6
Most certainly not. Have you tried on actual examples?

posted date: 2009-04-16 22:48:00


Re: PHP: Escape Quotes ONLY outside of HTML tags (Regex)#7
Its possible.You might be able to do it with regexp, with somethings similar to bellow. You will have to run it multiple times tho, as this regex replaces only 1 " with ( between tags.Serach: (\<.+?\>.+?)(")(.+?\</.+?\>)Replace: $1($3But, the better approach would be to utilise callbacks to do function replacement. Just create RE that sends content of tags to the function, which can then simply replace " with whatever you want. See more info here. Search for callback.As derobert noted, you might need to remove comments before that :)

posted date: 2009-04-17 02:45:00


Re: PHP: Escape Quotes ONLY outside of HTML tags (Regex)#8
You could try to split the string and separate the tags from the text data with this expression:<(?:\?[^?]+\?>|[A-Za-z]+(?:[^">]+|"[^"]*")*|!(?:\[CDATA\[(?:[^\]]+|](?:[^\]]|][^>]))*]]|--(?:[^-]+|-(?!->))*--))>This will (hopefully) match any XML PI, element tag, CDATA and comment block.So:$parts = preg_split((/(<(?:\?[^?]+\?>|[A-Za-z]+(?:[^">]+|"[^"]*")*|!(?:\[CDATA\[(?:[^\]]+|](?:[^\]]|][^>]))*]]|--(?:[^-]+|-(?!->))*--))>)/(, $str, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);$str = ((;foreach ($parts as $part) { if ($part[0] == (<() { $str .= $part; } else { $str .= str_replace(("(, (&quot;(, $part); }}But I doubt that this is very efficient. A real parser would be moreefficient and correct.

posted date: 2009-04-17 03:17:00


Re: PHP: Escape Quotes ONLY outside of HTML tags (Regex)#9
This doesn’t work if an attribute contains a >. This may not be common but it’s valid and therefore possible.

posted date: 2009-04-17 03:26:00


Re: PHP: Escape Quotes ONLY outside of HTML tags (Regex)#10
Hm... I would expect it to be escaped regardless. But you are right, in theory it is possible. +1 for the comment.

posted date: 2009-04-17 03:54:00


Re: PHP: Escape Quotes ONLY outside of HTML tags (Regex)#11
Having played around a bit with regex, I believe the following expression finds the valid end of the tag (always assuming valid HTML): /[^"<>]+((?:"[^"]*"|'[^']*')*[^">]*)*?>/ -- What do you think?

posted date: 2009-04-17 03:56:00


Re: PHP: Escape Quotes ONLY outside of HTML tags (Regex)#12
Even if you remove comments (how? with a parser, I assume), you're left with all kinds of fun with e.g., quoted strings. < and > are valid inside quoted strings. And I didn't even mention <script>, <style>, and <plaintext> blocks.

posted date: 2009-04-17 11:50:00


select page: « 1 2 »
Copyright ©2008-2017 www.momige.com, all rights reserved.