Jump to content

preg_replace problem, matching a string with non alphanumeric either side


rebajas

Recommended Posts

Hiya,

 

I have a preg_replace which matches a string with non alphanumeric characters either side (i.e not the middle of a word). The problem is, when I replace this with the desired replacement it replaces the non AN chars as well!

 

See code:

 

	$linkedKeywords = explode("|", $data[7]);
$i = 0;
foreach ($linkedKeywords As $val) {
	$replaceWith = "<a href=\"/search.php?keywordSearch=".urlencode($val)."\">".$val."</a>";
	$body = preg_replace("/\W".$linkedKeywords[$i]."\W/", $replaceWith, $body, 1);
	$i++;
}

 

Is there an efficient way to do what I want to do, like set the non alpha numeric characters as delimiters? If I haven't been clear then obviously I'd love to clarify these details.

 

T.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.