On 17/10/06, Al <[EMAIL PROTECTED]> wrote:
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.
>
> I will appreciate any help.
> Regards,
> Bunmi
Show us some examples of URL substrings, with any variations, you want to 
handle.

Most likely a regex function will do the job.

In 6 easy steps:

Step 1: Pinch  a regexp from perl...

 perl -e 'use Regexp::Common; print $RE{URI}{HTTP}, "\n";'

Step 2: Double up all backslashes

 M-x replace-string \ \\

Step 3: Escape single quote-marks

 M-x replace-string ' \'

Step 4. modify slightly to cope with the https scheme by adding an
optional 's' to the http scheme.

Step 5. add angle-brackets as delimiters

Step 6. use in a preg_replace()

<?php

$textString = 'orem ipsum dolor sit amet, consectetuer adipiscing
elit. Proin et urna. Duis quam. Suspendisse potenti. Etiam sem tortor,
ultricies nec,  http://example.com  imperdiet nec, tempus ac, purus.
Suspendisse id lectus. Nam vitae quam. Aliquam ligula nisl, vestibulum
vulputate, tempor nec, https://www.example.com  tincidunt sit amet,
libero. Suspendisse a justo. Cum sociis natoque penatibus et.';

$url_regexp = 
'<(?:(?:https?)://(?:(?:(?:(?:(?:(?:[a-zA-Z0-9][-a-zA-Z0-9]*)?[a-zA-Z0-9])[.])*(?:[a-zA-Z][-a-zA-Z0-9]*[a-zA-Z0-9]|[a-zA-Z])[.]?)|(?:[0-9]+[.][0-9]+[.][0-9]+[.][0-9]+)))(?::(?:(?:[0-9]*)))?(?:/(?:(?:(?:(?:(?:(?:[a-zA-Z0-9\\-_.!~*\'():@&=+$,]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*)(?:;(?:(?:[a-zA-Z0-9\\-_.!~*\'():@&=+$,]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*))*)(?:/(?:(?:(?:[a-zA-Z0-9\\-_.!~*\'():@&=+$,]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*)(?:;(?:(?:[a-zA-Z0-9\\-_.!~*\'():@&=+$,]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*))*))*))(?:[?](?:(?:(?:[;/?:@&=+$,a-zA-Z0-9\\-_.!~*\'()]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*)))?))?)>';

$output = preg_replace($url_regexp, '<a href="$0">$0</a>', $textString);

print $output;
?>

If http and https isn't enough for you, there's another more general
regexp but... well, it's 8.5Kb long.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to