note 38328 deleted from function.preg-replace-callback by felipe
| From: | felipe@php.net | Date: | Mon, 10 Dec 2007 02:23:04 +0000 |
| Subject: | note 38328 deleted from function.preg-replace-callback by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-133106@lists.php.net to get a copy of this message | ||
Note Submitter: spamhoneypot at acksys dot co dot uk
----
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>
*/
?>