Forum Moderators: coopster

Message Too Old, No Replies

Turning a string into a series of links

         

nfs2

3:39 pm on Jul 23, 2006 (gmt 0)

10+ Year Member



Say i pull some text from a database that says "this is text from a database"

I need to make each word a link, example;

<a href="http://www.mysite.com/search.php?=this">this</a>
<a href="http://www.mysite.com/search.php?=is">is</a>
<a href="http://www.mysite.com/search.php?=text">text</a>
<a href="http://www.mysite.com/search.php?=from">from</a>

And so on..

I can't seem to think of a way to do this that would be dynamic, so i could use it no matter how many words are returned from the origional query.

Can anyone help?

siMKin

6:35 am on Jul 24, 2006 (gmt 0)

10+ Year Member



<?php 
$txt = "This is some text. And this is some, ehm, more text :-)";
echo preg_replace("/\w+/U", "<a href=\"search.php?var=\\0\">\\0</a>", $txt);
?>

nfs2

4:28 am on Jul 26, 2006 (gmt 0)

10+ Year Member



Thanks, its almost working, but its making every letter a link, not just every word

If you or anyone knows how to fix it please let me know

coopster

11:08 am on Jul 26, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Surround the word characters with word boundaries and capture the word characters in a subpattern:
echo preg_replace("/\b(\w+)\b/", "<a href=\"search.php?var=$1\">$1</a>", $txt);

siMKin

7:54 am on Jul 27, 2006 (gmt 0)

10+ Year Member



Or simple leave out the U -> "/\w+/", thereby making it greedy. that should also do the trick
I first thought of another solution that required the Ungreedy modifier, and forgot to erase it

coopster

1:14 pm on Jul 27, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I originally had the "U" modifier on there as well (cut and pasted your code to test the issue) and forgot I peeled it off for a reason, just didn't change the replacement variables when testing. Removing the modifier is a great idea, eliminates the necessity of a subpattern match.
echo preg_replace("/\w+/", "<a href=\"search.php?var=$0\">$0</a>", $txt);

However, I just thought of something else -- since you are converting each word into html you may want to run it through htmlentities first to handle different character sets, etc. -- not to mention a good security measure.

$pattern = "/\w+/e"; 
$replacement = "'<a href=\"search.php?var=' . htmlentities('$0') . '\">' . htmlentities('$0') . '</a>'";
echo preg_replace($pattern, $replacement, $txt);

The "e" modifier evaluates the replacement string as PHP code and uses the result for replacing the search string.

Another variation of your search pattern would also handle certain characters that you may sometimes expect to see in words such as hyphens, ampersands, or an apostrophe --

<pre> 
<?php
$txt = "How about some Candy? We have M&M's, Take5 and sugar-free gum! :-)";
echo "$txt\n";
$pattern = "/\w[-'&\w]*/e";
$replacement = "'<a href=\"search.php?var=' . htmlentities('$0') . '\">' . htmlentities('$0') . '</a>'";
echo preg_replace($pattern, $replacement, $txt);
?>
</pre>

Just some additional thoughts to consider.