0

Hi guys, below are custom code to replacing text into link, for example

Google text, will be like this Google
Yahoo text, will be like this Yahoo

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']);

Edited by Ezzaral: Fixed code tags.

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by diafol
0
<?php
$msg = 'Google and Yahoo have a lot in common with Yahoo and Google.';
$orig[0] = '/Google/';
$orig[1] = '/Yahoo/';
$change[0] = '<a href="http://www.google.com">Google</a>';
$change[1] = '<a href="http://www.yahoo.com">Yahoo</a>';
echo preg_replace($orig, $change, $msg,1);
?>

Will that work? Not at a php processor at the mo so can't check it. The fourth parameter is the LIMIT. That will limit replacements to 1. If this doesn't work, try using a foreach loop with the array.

Edited by diafol: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.