URLs as Links with Regular Expressions using PHP Function.
Wall Script
Wall Script
Thursday, September 17, 2009

URLs as Links with Regular Expressions using PHP Function.

I'm using this PHP function to redirect text URLs as Links with Regular Expression validation. Just five lines of code, It's very useful. Take a look at this live demo .

URLs as Links with Regular Expressions using PHP Function.

To_Link.php
Contains PHP Code $text value validating with Regular Expressions
<?php
function To_Link($text)
{
$text = html_entity_decode($text);
$text = " ".$text;
$text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
'<a href="\\1">\\1</a>', $text);
$text = eregi_replace('(((f|ht){1}tps://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
'<a href="\\1">\\1</a>', $text);
$text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
'\\1<a href="http://\\2">\\2</a>', $text);
$text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})',
'<a href="mailto:\\1">\\1</a>', $text);
return $text;
}
?>



index.php
You have to include To_link.php. Using function To_Link('You Text and URL')
<?php
include('To_Link.php');

$text='jQuery and Ajax best 9lessons http://tinyurl.com/naottn';

echo To_Link($text);

?>

Related Article

Latest 20 Articles
web notification

8 comments:

  1. $text not $test ;)

    $test='jQuery and Ajax best 9lessons http://tinyurl.com/naottn';

    THX

    ReplyDelete
  2. ereg has been dropped from 5.3 and complettlly removed from php6, not a good idea to promote ereg

    ReplyDelete
  3. its a great idea srinivas ... regi_replace same as perl regular expression substitute

    ReplyDelete
  4. suggest a substitute of eregi_replace

    ReplyDelete
  5. Not work anymore on php 5.3, any idea? however i need replacement of the code.

    ReplyDelete
  6. actually i cant understand abt the full system can anybody explain please... i was finding but i didnt now please explain abt function and call it

    ReplyDelete
  7. ( ! ) Notice: Undefined index: state in D:\wamp\www\ifsc\index.php on line 33

    ReplyDelete

mailxengine Youtueb channel
Make in India
X