I iterate over a text string (in php) to change specific keywords in a database in links. The problem is that the words in the database exist inside each other, for example:
development, developer, development ...
As a result, I:
"Random string with the word <a href="developer"><a href="develop">develop</a>er</a> in it"
I would only need to wrap the tag around the developer , and not develop inside it ...
Here is my current function:
function hyper($haystack){
$query = "SELECT * FROM `hyperwords` ";
$query .= "WHERE `active` = '1' ";
$query .= "ORDER BY LENGTH(hyperword) DESC ";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$needle = $row['hyperword'];
$link = $row['link'];
$haystack = preg_replace("/($needle)/iu", "<a class='meta' href='$link'>$1</a>", $haystack);
}
return $haystack;
}
Thanks in advance!
source
share