View Full Version : How to make replacement, only replacing once

08-26-2011, 08:37 PM
Hi guys, below are custom code to replacing text into link, for example

Google text, will be like this Google (http://www.google.com)
Yahoo text, will be like this Yahoo (http://www.yahoo.com)

the replacement mod below are working well until now

now i just need a little fix again, to make it better again
i need it now only replacing once, so not too many links if the text appear few times

please share your knowledge guys, how to do that

$words = array(
'Google' => 'http://www.google.com',
'Yahoo' => 'http://www.yahoo.com'

$exp = array();
$rplc = array();
foreach ($words as $word => $url)
$exp[] = '/ ' . preg_quote($word) . ' /i';
$rplc[] = ' <argh href="' . $url . '">' . $word . '</argh> ';

$this->post['message'] = preg_replace($exp, $rplc, $this->post['message']);
$this->post['message'] = preg_replace_callback("#(<a [^>]*>)((?:[^<]|<(?!/?a>)|(?R))+)(</a>)#", create_function('$a', 'return $a[1] . preg_replace("#<argh[^>]*>(.*?)</argh>#", "$1", $a[2]) . $a[3];'), $this->post['message']);
$this->post['message'] = preg_replace(array('#<argh #', '#</argh>#'), array('<a ', '</a>'), $this->post['message']);

08-26-2011, 10:33 PM
I think you are talking about too many duplicates? if so, then array_unique (http://php.net/manual/en/function.array-unique.php) is what you need