Re: Re: How to recognise url in a block of text

From: Date: Wed, 18 Oct 2006 10:53:18 +0000
Subject: Re: Re: How to recognise url in a block of text
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-243278@lists.php.net to get a copy of this message
Hello Robin, I love you man. The solution you gave below just worked perfectly fine. Thanks to all who assisted me on this. I appreciate you all. Regards, Bunmi www.ayserve.net www.budelak.com Robin Vickery wrote:
On 17/10/06, Al <news@ridersite.org> 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.


« previous php.general (#243278) next »