Jump to content

Need URL Pattern


gaza165

Recommended Posts

Hello again guys!!

 

$linkpattern = "/http:\/\/[A-Za-z0-9\-_]+\\.+[A-Za-z0-9\.\/%&=\?\-_]+/";

 

Above is the pattern i am using to find links such as http://www.yahoo.com and http://www.google.com

 

Below is the patter i am using to find image links such as http://www.yahoo.com/image1.jpg or http://www.google.com/image2.png etc

 

$imgpattern = "%http://\S+\.(?:jpe?g|png|gif)%i";

 

-----------------------------------------------------------------------------

 

However when i print_r on matches found using the $linkpattern regex... it picks up the image url as well

 

Array ( [0] => Array ( [0] => http://www.ananova.com/images/web/1399921.jpg [1] => http://www.msn.com [2] => http://www.hotmail.com ) )

 

How do i get it so that the pattern only picks up actual urls instead of urls with an image extension??

 

Thanks

 

Garry Sheppard

Link to comment
https://forums.phpfreaks.com/topic/119756-need-url-pattern/
Share on other sites

well, this might work:

 

$str = array('http://www.google.com/image.jpg', 'http://www.domain.com/image1.png', 'http://www.domain.com/images/image2.png');
foreach($str as $val){
   preg_match('#http://.+(?=/.+)#i', $val, $matches);
   $matches[0] .= '/';
   echo $matches[0] . '<br />';
}

 

gives:

http://www.google.com/
http://www.domain.com/
http://www.domain.com/images/

 

If you don't want the final '/' character, just remove

$matches[0] .= '/';

 

Is that what you're looking for?

 

Cheers,

 

NRG

Link to comment
https://forums.phpfreaks.com/topic/119756-need-url-pattern/#findComment-617008
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.