Forum Moderators: coopster

Message Too Old, No Replies

Split string and make links from words escaping existing html elements

I need a way to split a string in words

         

yngens

2:15 am on Jan 12, 2011 (gmt 0)

10+ Year Member



I need a way to split a string, devided by comma (,) or new line and to display each word as a link. There is a similar request of 2007 ( [webmasterworld.com...] ), which is closed to commenting, so opening a new one.

The following code works fine for me:

<?php 
$array = explode(" ",$item['view']);
foreach ($array as $word) { print '<a href="/word/'.trim($word, ',.«»').'">'.$word.'</a> ';} ?>


However, with this code works for plaint text only. And I really would like to use this for the string that already contains some links or other html codes like <strong>, <b>, etc, and I would like them to be escaped. How can I do it?

[edited by: coopster at 3:41 pm (utc) on Jan 12, 2011]
[edit reason] fixed link [/edit]

jecasc

8:34 pm on Jan 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you want to remove any html code from the string you can use strip_tags:

[php.net...]