Re: Regex

From: Date: Wed, 23 Aug 2006 06:39:25 +0000
Subject: Re: Regex
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-241041@lists.php.net to get a copy of this message
M. Sokolewicz wrote:
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
You don't know Regex. Well, that's simple then, TRY to learn it. Noone will (or should) give you any answers if it's absolutely clear that you're not putting any effort into trying to find one yourself. "I know this should be done using Regex, but I don't know regex.", wouldn't you think it'd be a good idea to look up a tutorial somewhere or try to find out what this "regex" exactly is? Try to type regex in the php doc, see the notes for the various functions? really, a little more effort goes a long way. - tul
Hello, I know i MUST learn it.. time does not permit me. But surely i'll learn how to use this powerful tool! And if i didn't need a quick help from this ML, why i would be here ... do you think if time permitted me to learn this, i'll ask a little help from this ML ? I am learning and will continue to learn throughout my life. And remember, the day one says "i've learnt everthing now" this is the day his downfall begins ... Anyway i've got something working: function hyperlinks($text = '', $class = 'link') { $text = trim($text); if ($text != '') {
    if (trim($class) != '') $class = ' class="'.$class.'"';
    $in = array('((?:https?|ftp)://\\S+)(\\s|\\z)', '([[:alnum:]]([-_.]?[[:alnum:]])*@[[:alnum:]]([-_.]?[[:alnum:]])*\.([a-z]{2,4}))');
    $out = array('<a href="$1"'.$class.' target="_blank">$1</a>$2', '<a href="mailto:$1"'.$class.'>$1</a>');
    $text = preg_replace($in, $out, $text);
} return $text; } Renders http, https, ftp, mailto. Thanks M. Sokolewicz and everyone. Nadim Attari

« previous php.general (#241041) next »