Re: Extract links from strings

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

 



Jim Lucas wrote:
> 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
>>
>>
> 
> Something like this should work for you.
> 
> <plaintext><?php
> 
> $urls[] = 'Lorem ipsum dolor http://site.com sit amet';
> $urls[] = 'Lorem ipsum dolor https://www.site.com/ sit amet';
> $urls[] = 'Lorem ipsum dolor www.site1.net sit amet';
> $urls[] = 'Lorem ipsum dolor www site2.net sit amet';
> 
> foreach ( $urls AS $url ) {
> 	if ( preg_match('%((https?://|www\.)[^\s]+)%', $url, $m) ) {
> 		print_r($m);
> 	}
> }
> 
> ?>
> 

Actually, try this.  It seems to work a little better.

<plaintext><?php

$urls[] = 'Lorem ipsum dolor http://site.com sit amet';
$urls[] = 'Lorem ipsum dolor https://www.site.com/ or http://www.site2.com/';
$urls[] = 'Lorem ipsum dolor www.site1.net sit amet';
$urls[] = 'Lorem ipsum dolor www site2.net sit amet';

foreach ( $urls AS $url ) {
	if ( preg_match_all(	'%(https?://[^\s]+|www\.[^\s]+)%',
				$url,
				$m,
				(PREG_SET_ORDER ^ PREG_OFFSET_CAPTURE)
	) ) {
		print_r($m);
	}
}

?>


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