Page 1 of 1

Can anyone help me out

Posted: Mon Mar 23, 2009 2:53 am
by fawkes777
Hi all,

I'm using Apache 2.2 with php 5.2.8 and Windows base server (WAMPServer2)

I've got a dynamic string. On one day there can be 20 words and the other day maybe 40 words.

The string look like this (example only):

Code: Select all

$showline = "apples, banana, <strike>peaches</strike>, grapes, <a href=http://www.example.com target=_blank>pineapple</a>, apples-banana, banana, <a href=http://www.example.com target=_blank>pears-peaches</a>, pineapple.com, <strike>banana</strike>, pears.co.uk, pears";
I want to take each word and convert it into links, exp:

Code: Select all

$convert = "<a href=item.php?item=apples>apples</a>, <a href=item.php?item=banana>banana</a>, <strike>peaches</strike>, <a href=item.php?item=grapes>grapes</a>, <a href=http://www.example.com target=_blank>pineapple</a>, <a href=item.php?item=apples-banana>apples-banana</a>, <a href=item.php?item=banana>banana</a>, <a href=http://www.example.com target=_blank>pears-peaches</a>, <a href=item.php?item=pineapple.com>pineapple.com</a>, <strike>banana</strike>, <a href=item.php?item=pears.co.uk>pears.co.uk</a>, <a href=item.php?item=pears>pears</a>";
Note that the strike doesn't need to be link, the ones that are link doesn't need to be re-link and the pears.co.uk and pineapple.com still need to have the .co.uk and .com attached
Also note that the string need to have the html codings and that characters need to be included like the apples-banana

Is this possible? If anyone can please help me out it will be much appreciated

Regards
Shaun

Re: Can anyone help me out

Posted: Mon Mar 23, 2009 3:06 am
by requinix
Looks like regular expressions are the way to go.

Code: Select all

$new = preg_replace('/(^|,\s*+)(?!<a|<strike)([^,]+)/i', '$1<a href=item.php?item=$2>$2</a>', $old);

Re: Can anyone help me out

Posted: Mon Mar 23, 2009 3:12 am
by fawkes777
This work 100%, thank you so much Tasairis, you are just brilliant.