Question

I have the following code:

$content = eregi_replace('!([a-zA-Z0-9]+)', '<a href="http://www.example.com/link/\\1">\\1</a>', $content);

that works perfectly, but I wish it ignores repeated words.

e.g.:

!stack! stack! !overflow! !overflow

he should get only the first and ignore repeated, thus not making the link.

Was it helpful?

Solution

Use preg_replace() and it's limit parameter

$content = '!stack! stack! !overflow! !overflow';
$newcontent = preg_replace('#!([a-zA-Z0-9]+)#','<a href="http://www.example.com/link/\\1">\\1</a>',$content,1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top