At 10/17/2006 12:39 AM, AYSERVE.NET wrote:
Please, I need help on how to to recognise url in a block of text
being retrieved from a database and present it as a link within that text.
Here's an illustration of your problem:
Input:
Text text http://example.com/directory/filename.html#anchor text text
http://www.example.com/filename.php?arg=val&arg=val text text.
Output:
Text text <a
href="http://example.com/directory/filename.html#anchor">http://example.com/directory/filename.html#anchor</a>
text text <a
href="http://www.example.com/filename.php?arg=val&arg=val">http://www.example.com/filename.php?arg=val&arg=val</a>
text text.
Method:
- find each URL within the text
- replace each URL with an anchor tag that includes two copies of the
URL, one in the href attribute and one enclosed in the tag.
Technique:
I'd use Regular Expressions for this:
PHP - Regular Expression Functions (Perl-Compatible)
http://php.net/pcre
To write your own regexp to identify URIs, you'll want to read the spec:
RFC 1738 - Uniform Resource Locators (URL)
http://www.faqs.org/rfcs/rfc1738.html
Specifically you'll need to know what characters can and cannot occur
in an URL, and in what context, so that you can separate an URL from
surrounding text and punctuation such as parentheses, periods, commas, etc.
If you google "regular expression url" you'll find a lot of examples
of regexp's that find URLs, including:
Regex for URLs by Abigail
http://foad.org/~abigail/Perl/url2.html
Have fun,
Paul
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php