note 92456 added to function.preg-replace

From: Date: Fri, 24 Jul 2009 21:02:54 +0000
Subject: note 92456 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-158708@lists.php.net to get a copy of this message
I was writing a web crawler and wanted to limit its range to one website. I needed to dynamically escape the urls so I wrote the following function: <?php //Escape a string to be used as a regular expression pattern //Ex: escape_string_for_regex('http://www.example.com/s?q=php.net+docs') // returns http:\/\/www\.example\.com\/s\?q=php\.net\+docs function escape_string_for_regex($str) { //All regex special chars (according to arkani at iol dot pt below): // \ ^ . $ | ( ) [ ] // * + ? { } , $patterns = array('/\//', '/\^/', '/\./', '/\$/', '/\|/', '/\(/', '/\)/', '/\[/', '/\]/', '/\*/', '/\+/', '/\?/', '/\{/', '/\}/', '/\,/'); $replace = array('\/', '\^', '\.', '\$', '\|', '\(', '\)', '\[', '\]', '\*', '\+', '\?', '\{', '\}', '\,'); return preg_replace($patterns,$replace, $str); } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 92.48.15.75 ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace.php Edit -- https://master.php.net/note/edit/92456 Del: integrated -- https://master.php.net/note/delete/92456/integrated Del: useless -- https://master.php.net/note/delete/92456/useless Del: bad code -- https://master.php.net/note/delete/92456/bad+code Del: spam -- https://master.php.net/note/delete/92456/spam Del: non-english -- https://master.php.net/note/delete/92456/non-english Del: in docs -- https://master.php.net/note/delete/92456/in+docs Del: other reasons-- https://master.php.net/note/delete/92456 Reject -- https://master.php.net/note/reject/92456 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#158708) next »