|
Filed Under (PHP Code Samples) by Anoop on May-26-2007
Code (php) <? function makeClickableLinks($text) { $text = html_entity_decode($text); $text = " ".$text; $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\\1" target=_blank>\\1</a>', $text); $text = eregi_replace('(((f|ht){1}tps://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\\1" target=_blank>\\1</a>', $text); $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\\1<a href="http://\\2" target=_blank>\\2</a>', $text); $text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})', '<a href="mailto:\\1" target=_blank>\\1</a>', $text); return $text; } // Example Usage echo makeClickableLinks("This is a test clickable link: http://www.websewak.com You can also try using an email address like test@websewak.com"); ?>
Gera on August 27th, 2007 at 3:02 am #
Hello. I need some help regarding this topic (converting text to html, with links, using php). I have the following code that I have been working on. The problem is that this codes, inputs after every link in the text file, a // replace multiple spaces with single spaces // replace URLs with elements // start building output page .slug {font-size: 13pt; font-weight: bold} HEADER; // add page content // add page footer FOOTER; // display in browser // AND/OR // write output to a new .html file
Gera on August 27th, 2007 at 3:04 am #
Sorry It got cut up. I have the following code that I have been working on. The problem is that this codes, inputs after every link in the text file, the code adds a
Gera on August 27th, 2007 at 8:48 am #
Again, arrr!
brian on September 18th, 2007 at 2:16 am #
Thanks! this is exactly what I was looking for. There’s another one floating around that doesn’t work with simply “www.google.com”, it makes you type the http:// great job!
ZeroCool on February 17th, 2008 at 7:34 pm #
Thanks man, it work’s perfectly!
Yari on March 12th, 2008 at 3:40 am #
Thats a great script Anoop, i’ve modified it slightly to make sure it doesn’t convert EXISTING links, such as link. I’ve done this by adding a negate character class ([^]) before the main match: // http // https // www however, I couldn’t get this to work with converting emails… any ideas? Yari
Temistokles on June 11th, 2008 at 3:51 pm #
Yari: I think, this could work. $text = eregi_replace(’[^(href=|href=")](((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)’,
Chris on December 22nd, 2008 at 4:25 am #
Thanks!! Works fine.
Sushant Danekar on May 12th, 2009 at 3:19 pm #
Hi There, Is any idea regarding this requirement. REgards
AJ on May 12th, 2009 at 3:29 pm #
Hi Sushant, You can remove all html tags and make it text using the strip_tags() php function. You can find documentation here: http://in.php.net/strip_tags You can also provide the allowable tags if you would like to leave any tags. I hope this helps.
miza on June 15th, 2009 at 7:43 pm #
Thanks for that function! Much appreciated. Unfortunately I stumbled upon a problem when other characters are involved … like ä Ö etc.
miza on June 15th, 2009 at 7:44 pm #
ok, this comment was supposed to show the HTML-tag for the umlauts, sorry!
temistokles on June 29th, 2009 at 3:52 pm #
If I understand correctly, this could be the solution: $text = eregi_replace(’[^(href=|href=")](((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&;//=]+)’,
willy on July 8th, 2009 at 9:37 am #
I am not a programmer nor a developer, But I can do somethings. I have a social network for business professionals. In the Users section, the website: http://www.somesite.com does not appear clickable or active. How can I make this happen? This is what I presently have: To display the website entered by the new or existing member. I wanted to make it active or clickable.
animeez on August 24th, 2009 at 8:45 pm #
this script works great what if a user is not aware that the link can automatically parsed when putting a link when he input tags between the link? in this case, is it possible to create a script that strip all tags inputted by user manually then after that the text link will automatically parsed
miza on September 17th, 2009 at 8:51 pm #
temistokles, thanks for helping! simply escaping in the manner of \\” doesn’t help. Thanks a lot, Michael.
Sporsho on October 10th, 2009 at 12:38 am #
Nice…
John Moriarty on October 22nd, 2009 at 1:59 am #
the code at the top doesn’t work if there’s a “‘” in the URL either - e.g. http://www.johnlewis.com/Men/Men’s+Knitwear/Men’s+Knitwear/Men’s+Knitwear/6286/ProductCategory.aspx. Any suggestions of alterations to make it work?
Tom on January 6th, 2010 at 4:44 am #
eregi_replace is depreciated, here’s a version using preg_replace: function makeClickableLinks($text) $text = html_entity_decode($text);
Tom on January 6th, 2010 at 4:46 am #
oops, change all “$txt” to “$text” in the above. (cursed find replace)
IMWD on February 4th, 2010 at 9:54 pm #
Is there a way to implement this for use with a variable? I’ve got a table that draws an email from the database but displays it as text. If I do this: echo “makeClickableLinks $line[2]“; it displays this: makeClickableLinks somebody@email.com rather than as a link. Any ideas? Many thanks.
IMWD on February 4th, 2010 at 10:42 pm #
In my posting above, the sample email was rendered as a link. It is not so on the web page. It is just plain text. Post a comment
|
|