Re: Extract links from strings

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



At 12:52 PM -0300 9/21/09, Jônatas Zechim wrote:
Hi there, i've the following strings:

$string1 = 'Lorem ipsum dolor http://site.com sit amet';
$string2 = 'Lorem ipsum dolor http://www.site.com/ sit amet';
$string3 = 'Lorem ipsum dolor http://www.site.net sit amet';

How can I extract the URL from these strings?
They can be [http:// + url] or [www. + url].

Zechim


Not tested:

<?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;
?>

Cheers,

tedd

--
-------
http://sperling.com  http://ancientstones.com  http://earthstones.com

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



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux