I'm looking for a mini function like this example; ourfunction("our four words sentence") = <a href="/tag/our.html">our</a> <a href="/tag/four.html">four</a> <a href="/tag/words.html">words</a> <a href="/tag/sentence.html">sentence</a> PHP: Can someone show a function like this? Thanks.
function ourfunction($words) { $word = explode(" ", $words); foreach($word as $value) { //return "<a href='/tab/$value.html'>$value</a> "; echo "<a href='/tab/$value.html'>$value</a> "; } } ourfunction("our four words sentence"); PHP: That should work.
EDIT: A bit too late. Like this? function words2links($text) { $output = array(); foreach (explode(' ', $text) AS $word) { if ($word = preg_replace('/\W/', null, $word)) { $output[] = '<a href="/tag/' . $word . '.html">' . $word . '</a>'; } } return implode(' ', $output); } echo words2links('hello this is a test .'); PHP: