Can anyone help me out

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fawkes777
Forum Newbie
Posts: 3
Joined: Mon Mar 23, 2009 2:25 am

Can anyone help me out

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Can anyone help me out

Post 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);
fawkes777
Forum Newbie
Posts: 3
Joined: Mon Mar 23, 2009 2:25 am

Re: Can anyone help me out

Post by fawkes777 »

This work 100%, thank you so much Tasairis, you are just brilliant.
Post Reply