Re: [PHP] Is there a faster way to escape special characters in a regex?
On Tuesday 12 March 2002 22:30, Andrey Hristov wrote: > Yes they are identical. The rule is that the first and the last(excluding > modificators) symbol must be identical so > > |abc| > > ~abc~ > /abc/ > %abc%(not sure for that) should be ok. > are equivalent. The docs uses // syntax because it is the most popular. The delimiter could be anything as long as it's not alphanumeric or a backslash. -- Jason Wong -> Gremlins Associates -> www.gremlins.com.hk /* I think we're in trouble. -- Han Solo */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Is there a faster way to escape special characters in a regex?
Yes they are identical. The rule is that the first and the last(excluding modificators) symbol must be identical so |abc| ~abc~ /abc/ %abc%(not sure for that) are equivalent. The docs uses // syntax because it is the most popular. Regards, Andrey Hristov - Original Message - From: "Richard Davey" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Tuesday, March 12, 2002 4:26 PM Subject: Re: [PHP] Is there a faster way to escape special characters in a regex? > "Andrey Hristov" <[EMAIL PROTECTED]> wrote in message > 053801c1c9d0$5b4da400$0b01a8c0@ANDreY">news:053801c1c9d0$5b4da400$0b01a8c0@ANDreY... > > > In PHP 4.0.5 and later, every parameter to str_replace() can be an > array.If search and > > Thank you for pointing that out :) > > I've now changed my code from the (rather lengthy) version to the following > which worked perfectly for me: > > $regcheck = array("/","\\","^",".","[","]","$","(",")","*","?","{","}"); > $regreplace = > array("","","\^","\.","\[","\]","\$","\(","\)","\*","\?","\{","\}"); > $badwordtest = "/" . str_replace($regcheck,$regreplace,$badwords[$i]) . > "/i"; > > > BTW /abc/ and ~abc~ are equivalent regexes. So get a character which will > not occur in the string and put in the front and the end. > > Does this mean that ~myword~ and /myword/ are identical? > I only used /myword/ because it's in the php manual example like that. > > Cheers, > > Rich > -- > Fatal Design > http://www.fatal-design.com > Atari / DarkBASIC / Coding / Since 1995 > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Is there a faster way to escape special characters in a regex?
"Andrey Hristov" <[EMAIL PROTECTED]> wrote in message 053801c1c9d0$5b4da400$0b01a8c0@ANDreY">news:053801c1c9d0$5b4da400$0b01a8c0@ANDreY... > In PHP 4.0.5 and later, every parameter to str_replace() can be an array.If search and Thank you for pointing that out :) I've now changed my code from the (rather lengthy) version to the following which worked perfectly for me: $regcheck = array("/","\\","^",".","[","]","$","(",")","*","?","{","}"); $regreplace = array("","","\^","\.","\[","\]","\$","\(","\)","\*","\?","\{","\}"); $badwordtest = "/" . str_replace($regcheck,$regreplace,$badwords[$i]) . "/i"; > BTW /abc/ and ~abc~ are equivalent regexes. So get a character which will not occur in the string and put in the front and the end. Does this mean that ~myword~ and /myword/ are identical? I only used /myword/ because it's in the php manual example like that. Cheers, Rich -- Fatal Design http://www.fatal-design.com Atari / DarkBASIC / Coding / Since 1995 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Is there a faster way to escape special characters in a regex?
In PHP 4.0.5 and later, every parameter to str_replace() can be an array.If search and replace are arrays, then str_replace() takes a value from each array and uses them to do search and replace on subject. If replace has fewer values than search, then an empty string is used for the rest of replacement values. If search is an array and replace is a string; then this replacement string is used for every value of search. The converse would not make sense, though. BTW /abc/ and ~abc~ are equivalent regexes. So get a character which will not occur in the string and put in the front and the end. Regards, Andrey Hristov - Original Message - From: "Richard Davey" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Tuesday, March 12, 2002 3:57 PM Subject: [PHP] Is there a faster way to escape special characters in a regex? > Hi all, > > At the moment I'm doing this to escape all "special" regular expression > characters from my regex string: > >$badwordtest = str_replace("/","",$badwordtest); >$badwordtest = str_replace("\\","",$badwordtest); >$badwordtest = str_replace("^","\^",$badwordtest); >$badwordtest = str_replace(".","\.",$badwordtest); >$badwordtest = str_replace("[","\[",$badwordtest); >$badwordtest = str_replace("$","\$",$badwordtest); >$badwordtest = str_replace("(","\(",$badwordtest); >$badwordtest = str_replace(")","\)",$badwordtest); >$badwordtest = str_replace("|","\|",$badwordtest); >$badwordtest = str_replace("*","\*",$badwordtest); >$badwordtest = str_replace("+","\+",$badwordtest); >$badwordtest = str_replace("?","\?",$badwordtest); >$badwordtest = str_replace("{","\{",$badwordtest); >$badwordtest = str_replace("}","\}",$badwordtest); >$badwordtest = str_replace("\\","\\",$badwordtest); >$badwordtest = "/" . $badwordtest . "/i"; > >if (preg_match($badwordtest,$word)) { > $directmatch = TRUE; >} > > Is there a way to either do this all in one go or even better, have the ereg > itself ignore those characters if they are contained in the string? The idea > is that I want to check a word for swearing by doing a direct comparison > with a list of swear words, however the word I check could easily contain a > special character so I need to escape it before the test commences. > > Suggestions very welcome! > > Cheers, > > Richard > -- > Fatal Design > http://www.fatal-design.com > Atari / DarkBASIC / Coding / Since 1995 > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php