954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Wrapping an <a> tag around a web address that's inside a paragraph

Hi all

I've been trying to figure out how I would accomplish my problem mentioned in the titl of this thread with no luck. I've looked at a combination of using strstr, str_replace, substr and strpos.

What I'd like to do is manipulate a paragraph so that if there is a web address in it, it gets an tag wrapped around it with the href pointing to the web address.

Can anyone give me some suggestions please? :)

Venom Rush
Posting Whiz
353 posts since Oct 2007
Reputation Points: 31
Solved Threads: 2
 

you would need to use a regex and the preg_replace function.

here just found this:

$text = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $text);


remember google is your friend

kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

you would need to use a regex and the preg_replace function.

here just found this:

$text = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $text);

remember google is your friend


Thanks for that kkeith29. Could you possibly give me the link to where you found that. There might be cases where the link is without the http:// portion and I can't seem to figure out how to alter the above code.

P.S. Google doesn't love me...because it found out I use Yahoo as well.

Venom Rush
Posting Whiz
353 posts since Oct 2007
Reputation Points: 31
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You