note 38328 added to function.preg-replace-callback

From: Date: Tue, 16 Dec 2003 23:08:29 +0000
Subject: note 38328 added to function.preg-replace-callback
Groups: php.notes 
Request: Send a blank email to php-notes+get-62097@lists.php.net to get a copy of this message
I thought I'd post this as I'd been using the function preg_replace_callback with NP within functions, but as soon as I re-wrote my script as a class, it all fell apart. When debugging I ended up writing test code and this is what follows. It works as I'd expect, YMMV of course. <?php // v v over simplified class foo { function parse() { $pattern = "/<a(.*?)href\s*=\s*['|\"|\s*](.*?)['|\"|>](.*?)>(.*?)<\/a>/i"; $string = "<a class='whatever' href='http://foo.com' target='_blank'>foo</a>"; print preg_replace_callback($pattern,array($this,'cb'),$string); } function cb($matches) { return "<a" . $matches[1] . "href='http://someothersite.com/foo.php?page=" . urlencode($matches[2]) . "'" . $matches[3] . ">" . $matches[4] . "</a>"; } } $bar = new foo(); $bar->parse(); /** output is <a class='whatever' href='http://someothersite.com/foo.php?page=http%3A%2F%2Ffoo.com' target='_blank'>foo</a> */ ?> ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace-callback.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+38328 Delete -- http://master.php.net/manage/user-notes.php?action=delete+38328&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+38328&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#62097) next »