How to transform normal text into link

This is a discussion on "How to transform normal text into link" within the PHP Forum section. This forum, and the thread "How to transform normal text into link are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Sep 5th, 2006, 09:56
Junior Member
Join Date: May 2006
Location: Lebanon
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
How to transform normal text into link

Hello,

I am wondering how we could transform a normal text into a link using PHP.

What I mean is:

- the user submits a form with the text http://www.yahoo.com in it,

- when he goes to see his submitted text, I want "http://www.yahoo.com" to be, in HTML, "<a href=http://www.yahoo.com>http://www.yahoo.com</a>", or, in other words, a link to Yahoo.com.

I have been trying for hours...

Any help is fully appreciated.

Thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Sep 5th, 2006, 10:18
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How to transform normal text into link

If his input is in a form element called "URL", then:

$sayvar = htmlspecialchars(stripslashes($_REQUEST[url]));
$result = "<a href=\""$sayvar\">$sayvar</a>";
print $result;

Note - it could be much shorter, but the code above deals with some security issues if you have malicious users.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Sep 11th, 2006, 11:35
Junior Member
Join Date: May 2006
Location: Lebanon
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How to transform normal text into link

Yes, I knew how to do that, it's easy... but if the submitted element contains other text than just the link, then what do I do?

Example: "Check out http://www.google.com to search the Internet."

How do I transform normal text "http://www.google.com" in a link?

Robert
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Sep 11th, 2006, 12:36
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How to transform normal text into link

What is the source of the text?

Check out stristr(). I think you can probably write this yourself. I'd do a little more but I don't know how to use a wildcard in stristr(). There might be a better function but that will get you pointed right.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Sep 11th, 2006, 14:19
Junior Member
Join Date: May 2006
Location: Lebanon
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How to transform normal text into link

Ok, so let me re-explain my problem:

Using a form, the user submits normal text to the server through a textarea. Ex: "I love http://www.google.com because it's simple."

If he submits a link, like in the example above, I want PHP to identify it and transform it into an actual link that the user can click when viewing his submitted text. I want PHP to scan the text, locate "http://" and transform it into "<a href=...>", and add an "</a>" at the end of the link.

Just like on this forum, when you submit a reply containing http://www.google.com, it actually transforms it into a true, clickable link that user can click on when displayed on the thread.

Any ideas?

Thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Sep 11th, 2006, 17:37
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How to transform normal text into link

You are going to have to parse the text using regex to look for a sub-string that is a valid hyperlink.

Trouble is you could find a sub-string that is a valid hyperlink befor you have actually got to the end of the intended hyperlink.

E.g.
http://blah.org and http://blah.org.uk are both valid.

You could of course, having recognised valid start points for a hyperlink assume that it will end with the first space. Having retrieved that sub-string, you could then validate that as a hyperlink.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Sep 11th, 2006, 20:20
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How to transform normal text into link

Bah. My feeble braincell just realized.

Code: Select all
$txt = $_POST ['name'];
$urls = explode(" ",$txt);
foreach ($urls as $value) {
$htp = substr($value, 0, 7);
    if ($htp == "http://")  {
                        $link = substr($value, 7);
                        echo ('<a href="' . $value . '">' . $link . '</a>');
    }
}
Piece of cake using explode(). There are some holes in it, but maybe some form validation would fill a few of them. Anything but a space before the citation will completely mess it up, so you might want to strip out some common formatting expressions like quotation marks, tabs. etc.

Also, it would go a long way if you simply tell them to make sure there is a single space before any url.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Sep 12th, 2006, 13:29
Junior Member
Join Date: May 2006
Location: Lebanon
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How to transform normal text into link

Hi masonbarge,

Indeed, that is a cool code, but it only returns the links, not the whole text in itself. How can I return the whole text (with the links inside)?

Also, can't we find such a code in open source forums like this one (or phpBB). I've been looking into their sources but have found nothing yet.

Last edited by robertboyle; Sep 12th, 2006 at 13:32.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Sep 12th, 2006, 20:35
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How to transform normal text into link

Code: Select all
$txt = $_POST ['name'];
$urls = explode(" ",$txt);
foreach ($urls as $value) {
$htp = substr($value, 0, 7);
    if ($htp == "http://")  {
                        $link = substr($value, 7);
                        echo ('<a href="' . $value . '"> ' . $link . '</a>');
    } else {
            echo " $value";
}
Notice the space in [["_$value" ]]and also [[$value . '">_' ]] .
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old Sep 13th, 2006, 12:44
Junior Member
Join Date: May 2006
Location: Lebanon
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Re: How to transform normal text into link

OK, so now the whole text is returned with the link in it. Thanks a lot, MasonBarge! (you might wanna add a final } in the code to close the foreach).

Also, anyone that finds a more secure, more performant code, please post it.

Thanks

Robert
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
transform, normal, text, link

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] transform the xml to html with php and xsl checkmate PHP Forum 4 Nov 5th, 2007 01:36
Buy One Ad or Text Link and Get One Free march Link Building and Link Sales 1 Oct 25th, 2007 15:37
How do I insert link in to text Karenm Starting Out 2 Jun 12th, 2007 17:00
Transform to CSS tolis Web Page Design 1 Jul 15th, 2006 06:33


All times are GMT. The time now is 02:23.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42