note 71469 added to function.preg-match

From: Date: Thu, 30 Nov 2006 10:05:19 +0000
Subject: note 71469 added to function.preg-match
Groups: php.notes 
Request: Send a blank email to php-notes+get-120183@lists.php.net to get a copy of this message
Searching for a way to figure out how to make patterns in the preg_match command, I tried figuring out any piece of the patterns of others, and came to some conclusions... to try something out, i tried to make a pattern that could read if a backslash ( \ ) was used before a single quote ( ' ). I ended up with a pattern that read if any quotes where there at all, in any way possible. Maybe people could also use this, but if you see what i did wrong, please do tell me: <?php $string1 = "a \'quoted\' \"string\""; $string2 = "a 'quoted' string"; $string3 = "a &#039;quoted&#039; &quot;string&quot;"; $string4 = "a not-quoted string"; if (preg_match("/[\\'!']|[\\\"!\"]/",$string1)) { echo $string1.' wrong way'."\n"; } else { echo $string1.' right way'."\n"; } // returns: a \'quoted\' "string" wrong way if (preg_match("/[\\'!']|[\\\"!\"]/",$string2)) { echo $string2.' wrong way'."\n"; } else { echo $string2.' right way'."\n"; } // returns: a 'quoted' string wrong way if (preg_match("/[\\'!']|[\\\"!\"]/",$string3)) { echo $string3.' wrong way'."\n"; } else { echo $string3.' right way'."\n"; } // returns: a 'quoted' "string" right way if (preg_match("/[\\'!']|[\\\"!\"]/",html_entity_decode($string3))) { echo html_entity_decode($string3).' wrong way'."\n"; } else { echo html_entity_decode($string3).' right way'."\n"; } // returns: a 'quoted' "string" wrong way if (preg_match("/[\\'!']|[\\\"!\"]/",$string4)) { echo $string4.' wrong way'."\n"; } else { echo $string4.' right way'."\n"; } // returns: a not-quoted string right way ?> ---- Server IP: 83.137.20.120 Probable Submitter: 82.92.27.234 ---- Manual Page -- http://www.php.net/manual/en/function.preg-match.php Edit -- https://master.php.net/note/edit/71469 Del: integrated -- https://master.php.net/note/delete/71469/integrated Del: useless -- https://master.php.net/note/delete/71469/useless Del: bad code -- https://master.php.net/note/delete/71469/bad+code Del: spam -- https://master.php.net/note/delete/71469/spam Del: non-english -- https://master.php.net/note/delete/71469/non-english Del: in docs -- https://master.php.net/note/delete/71469/in+docs Del: other reasons-- https://master.php.net/note/delete/71469 Reject -- https://master.php.net/note/reject/71469 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#120183) next »