Re: Regex
| From: | Ivo F.A.C. Fokkema | Date: | Mon, 21 Aug 2006 15:22:15 +0000 |
| Subject: | Re: Regex | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-240950@lists.php.net to get a copy of this message | ||
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