pre_replace multi-dimensional array problem
Posted
by Martin
on Stack Overflow
See other posts from Stack Overflow
or by Martin
Published on 2010-06-18T10:48:50Z
Indexed on
2010/06/18
10:53 UTC
Read the original article
Hit count: 222
I want to replace word groups by links. I use a multi-dimensional array to define these (in the real world there will be thousands of them). Here's the code:
$text = "<html><body><pre>
Here is Foo in text.
Now come Baz? and Bar-X.
Replace nothing here: Foo (followed by brackets).
</pre></body></html>";
$s = array(
array("t" => "Foo", "u" => "http://www.foo.com", "c" => "foo"),
array("t" => "Baz?", "u" => "http://www.baz.net", "c" => "test"),
array("t" => "Bar-X", "u" => "http://www.baz.org", "c" => "test")
);
foreach ($s as $i => $row) {
$replaced = preg_replace('/(?=\Q'.$row["t"].'\E[^(]+$)\b\Q'.$row["t"].'\E\b/m',
'<a href="'.$row["u"].'" class="'.$row["c"].'">'.$row["t"].'</a>',
$text);
}
echo $replaced;
?>
The problem is that only one array element is replaced and not all. It's something about $text in peg_replace(). Anyone got a hint for me? Thanks!
© Stack Overflow or respective owner