Re: Regex

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, 21 Aug 2006 13:51:16 +0400, Nadim Attari wrote:

> Hello,
> 
> I have some text in a table... the text contains hyperlinks (but not 
> html coded, i.e. plain "Some text...http://www.something.com";)
> 
> When i retrieve these texts from the table, i want the hyperlinks to 
> become clickable, i.e. <a href etc added automatically.
> 
> "Some text...<a 
> href="http://www.something.com";>http://www.something.com</a>"
> 
> I know this sould be done using Regex, but i don't know regex.
> 
> Any help (links, examples, etc)
> 
> Thanks
> Nadim Attari

How's this:

<?php
// $s contains the entry from the table.

// Non-strict url matching.
$s = preg_replace('/(http:\/\/[^\s]+)/i', "<A href=\"$1\">$1</A>", $s);
?>

This is very non strict. Anything starting with http:// until the next
whitespace (\s) is clickable. You might want to put a more strict rule in
there, but it depends on the text your searching in. Note that above code
does not work when an url is at the end of a line, and followed by a
period (.).

Ivo

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux