Hi

I found a nifty regex on the web that i have used to turn a url into an active url in the middle of a text string :

$str = preg_replace('/\b(https?|ftp|file|http)[:\/\/][-A-Za-z0-9+&@#\/%?=~_|!:,.;]*[-A-Za-z0-9+&@#\/%=~_|]/', '<a class=\"weblink\"  href="\0">\0</a>', $str);
//
//

i was wondering if it anyone could think of a way to remove the "http://" from the link displayed in the text, so that the function would output

<a class="weblink"  href="http://www.google.com">www.google.com</a>

instead of how it does at the moment :

<a class="weblink"  href="http://www.google.com">http://www.google.com</a>

I would need to do something to the >\0<\a> but i can't find a way to do this

thanks for any ideas you may have on the subject

    You could use something like this:

    $str = preg_replace('/\b(https?|ftp|file|http):\/\/([-A-Za-z0-9+&@#\/%?=~_|!:,.;]*[-A-Za-z0-9+&@#\/%=~_|])/', '<a class=\"weblink\"  href="\0">\2</a>', $str);

      wow! I didn't know that several bits of the string could be extracted from the same regex - i'd like to read some more about that : what's it called when you refer to part of the regex matched string using a number as a sort of placeholder ?

      and it works perfectly (as usual, lol!)

      thanks for your help

        wow! I didn't know that several bits of the string could be extracted from the same regex - i'd like to read some more about that : what's it called when you refer to part of the regex matched string using a number as a sort of placeholder ?

        Refer to the "Subpatterns" section of the PHP manual's entry on PCRE Details.

          Write a Reply...