gaza165 Posted August 14, 2008 Share Posted August 14, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/119756-need-url-pattern/ Share on other sites More sharing options...
nrg_alpha Posted August 15, 2008 Share Posted August 15, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/119756-need-url-pattern/#findComment-617008 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.