PDA

View Full Version : change http to links



jazzman121
September 25th, 2003, 05:26 PM
say i have a varable

$info

what i want to do is find any word begining with http:// and change it to a link by adding the code <a herf u know wat i mean

how do i do that?
i think i could use preg_replace but how do i use that funtion ?

jazzman121
September 25th, 2003, 05:37 PM
its ok thanks anyways i found it out ... this code is good coz if some (smart html dude ) puts in the <a herf code it wont mess up the link by adding another <a herf


$txt = preg_replace( "/(?<!<a href=\")((http|ftp)+(s)?:\/\/[^<>\s]+)/i", "<a href=\"\\0\">\\0</a>", $txt );

jazzman121
September 25th, 2003, 06:19 PM
can someone explain this to me please i dont understand how it works but it works fine thou ...

im trying to make this soo that if anyone types www.watever.com also it will change it to a link please help

Jubba
September 25th, 2003, 06:31 PM
this might help: http://us4.php.net/pcre

jazzman121
September 26th, 2003, 03:13 PM
I read that full page... it still doesnt explain the whole

"/(?<!<a href=\")((http|ftp)+(s)?://[^<>s]+)/i


part i dont get it...

Jubba
September 26th, 2003, 03:40 PM
this one might help more: http://www.webcom.com/glossary/regexp.shtml

sorry about that. If not then search google for "Regular Expression Explanation"

I dont really know that much about regular expressions or Iw ould explain it for you. :sigh:

jazzman121
September 27th, 2003, 08:31 AM
thanks a lottt that helped :) and just in case anyone wants to know to find any www.watever.com and replace it by a link use this


$text = preg_replace( "/www.+/i", "[<a href=\"\\0\" target=\"_blank\">www</a>]", $text );


and that link u gave me is good but i found one mcuhhhhh bettter on google

check it out

http://www.tote-taste.de/X-Project/regex/index.php

Jubba
September 27th, 2003, 09:34 AM
awesome thanks. I apologize for not being able to find a really good one for you, I was really busy yesterday. I'm glad you found a good one.

jazzman121
September 27th, 2003, 09:53 AM
Noo dude dont apologize you led me in the right direction and thats how i figured it out, you were great help.... thats exactly what i was looking for, coz i dint know what those special charactres were called .... but yea thanks for your help :)