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.
|
|
|
|
|
![]() |
||
How to transform normal text into link
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
#1
|
|||
|
|||
|
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. |
|
|
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
||||
|
||||
|
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. |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
|||
|
|||
|
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. |
|
#7
|
||||
|
||||
|
Re: How to transform normal text into link
Bah. My feeble braincell just realized.
Also, it would go a long way if you simply tell them to make sure there is a single space before any url. |
|
#8
|
|||
|
|||
|
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. |
|
#9
|
||||
|
||||
|
Re: How to transform normal text into link
|
|
#10
|
|||
|
|||
|
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 |
![]() |
| Tags |
| transform, normal, text, link |
| Thread Tools | |
|
|
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 |