afrojojo Posted June 18, 2010 Share Posted June 18, 2010 Need some help extacting the first url from a string. Whether it would start with http or https. Thanks Input this is some text http://yahoo.com/directory/file.php?id=1&blah=1 this is some texthttp://msn.com this is some text this is some text https://google.com. Output http://yahoo.com/directory/file.php?id=1&blah=1 Quote Link to comment Share on other sites More sharing options...
premiso Posted June 18, 2010 Share Posted June 18, 2010 http://www.phpfreaks.com/forums/index.php?topic=294975.0 Sometimes a search of the forums works wonders. You will also find many other topics in the regex section overing this. Quote Link to comment Share on other sites More sharing options...
afrojojo Posted June 18, 2010 Author Share Posted June 18, 2010 http://www.phpfreaks.com/forums/index.php?topic=294975.0 Sometimes a search of the forums works wonders. You will also find many other topics in the regex section overing this. That's not what I asked. Quote Link to comment Share on other sites More sharing options...
premiso Posted June 18, 2010 Share Posted June 18, 2010 Using that matching expression posted in the preg_replace, use it in preg_match and viola, you have the first url matched. Simple as that. Quote Link to comment Share on other sites More sharing options...
afrojojo Posted June 18, 2010 Author Share Posted June 18, 2010 Using that matching expression posted in the preg_replace, use it in preg_match and viola, you have the first url matched. Simple as that. Doesn't work Quote Link to comment Share on other sites More sharing options...
cags Posted June 18, 2010 Share Posted June 18, 2010 With helpful information such as that, and an obvious attitude problem, I believe we will have hammered out an accurate answer for you by the 4th June 2020. Quote Link to comment Share on other sites More sharing options...
afrojojo Posted June 18, 2010 Author Share Posted June 18, 2010 http://www.phpfreaks.com/forums/index.php?topic=294975.0 Sometimes a search of the forums works wonders. You will also find many other topics in the regex section overing this. I've searched. I couldn't find anything. Thats not what my question was related to. http://www.phpfreaks.com/forums/index.php?topic=294975.0 Sometimes a search of the forums works wonders. You will also find many other topics in the regex section overing this. Using that matching expression posted in the preg_replace, use it in preg_match and viola, you have the first url matched. Simple as that. I tried that. Doesn't work. Quote Link to comment Share on other sites More sharing options...
ZachMEdwards Posted June 18, 2010 Share Posted June 18, 2010 <?PHP $input = 'this is some text http://yahoo.com/directory/file.php?id=1&blah=1 this is some texthttp://msn.com this is some texthis is some text https://google.com.'; $pattern = '/((?:https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$])/i'; if(preg_match($pattern, $input, $match)) { echo $match[0]; } else { echo 'Not found!'; } ?> Quote Link to comment Share on other sites More sharing options...
afrojojo Posted June 18, 2010 Author Share Posted June 18, 2010 Thanks Zach Quote Link to comment Share on other sites More sharing options...
afrojojo Posted June 18, 2010 Author Share Posted June 18, 2010 One more question. Is there a way to choose which match in the string it gets? match[1] = The first url in string match[2] = The second url in string Quote Link to comment Share on other sites More sharing options...
salathe Posted June 19, 2010 Share Posted June 19, 2010 ajfrojojo, that's not how preg_match works; please read the appropriate manual page to see why. There is, however, a very similarly named function which would help lots with what you're after; I'll leave you to discover that on your own after reading the link above. Quote Link to comment Share on other sites More sharing options...
afrojojo Posted June 19, 2010 Author Share Posted June 19, 2010 $pattern = '/((?:https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$])/i'; if(preg_match_all($pattern, $string, $match)) { $string=$match[0][0]; echo $string; } I came up with that using preg_match_all. It's almost what I want. $string = "<a href="http://google.com">Google</a> this is some text http://yahoo.com this is some texthttp://msn.com"; If the string was what I have above, how do I make the pattern stop at a " or '. Otherwise the first string produced would be http://google.com">Google</a>. I just want it to be http://google.com. It only stops at the first space. I would like it to stop at the first space, the first ", or the first '. So I would want the output of $string=$match[0][0] to be http://google.com. The output of $string=$match[0][1] to be http://yahoo.com, and so on. Quote Link to comment Share on other sites More sharing options...
salathe Posted June 20, 2010 Share Posted June 20, 2010 The pattern that you posted will stop at a quote. Quote Link to comment Share on other sites More sharing options...
afrojojo Posted June 21, 2010 Author Share Posted June 21, 2010 I suppose I should rephrase this. I want it to stop at the first space, the SECOND quote, or the SECOND apostrophe, and begin at http:// or https:// Quote Link to comment Share on other sites More sharing options...
salathe Posted June 21, 2010 Share Posted June 21, 2010 Show us some sample string and code (may be the same as the previous snippet) which does not behave as you want, then be precise about how you want it to behave. Currently, the code and string in your next-to-last post work as advertised. Quote Link to comment Share on other sites More sharing options...
afrojojo Posted June 21, 2010 Author Share Posted June 21, 2010 $pattern = '/((?:https):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$])/i'; if(preg_match_all($pattern, $string, $match)) { $string=$match[0][0]; echo $string; } Input: http://yahoo.com this is some text Output http://yahoo.com The above is ok Input <a href="http://yahoo.com">Yahoo</a> Output http://yahoo.com" I want to remove that last quote. Quote Link to comment Share on other sites More sharing options...
salathe Posted June 21, 2010 Share Posted June 21, 2010 Neither of those inputs should output anything since the regex in your code looks for https Quote Link to comment Share on other sites More sharing options...
afrojojo Posted June 22, 2010 Author Share Posted June 22, 2010 Oops... /((?:https?):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$])/i Quote Link to comment Share on other sites More sharing options...
salathe Posted June 22, 2010 Share Posted June 22, 2010 The second input cannot output with a double-quote since the regex in your code cannot, ever match a double-quote. Please, write show us a sample script (like what you have already, but with $string(s) being defined) that I can run, which gives you that trailing double-quote character in the match. Quote Link to comment Share on other sites More sharing options...
afrojojo Posted June 22, 2010 Author Share Posted June 22, 2010 $string = '<a href="http://yahoo.com">Yahoo</a>'; $pattern = '/((?:https?):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$])/i'; if(preg_match_all($pattern, $string, $match)) { $string=$match[0][0]; echo $string; } Output http://yahoo.com">Yahoo</a> Quote Link to comment Share on other sites More sharing options...
salathe Posted June 22, 2010 Share Posted June 22, 2010 I cannot reproduce your output, nor can I see in your code how on earth that text can get matched. See a live running example of your code (behaving properly) -- http://codepad.viper-7.com/W32a4S Are you sure you're not just doing something dumb like echoing the original string rather than what was matched? Quote Link to comment Share on other sites More sharing options...
afrojojo Posted June 23, 2010 Author Share Posted June 23, 2010 I figured out the issue. I applied htmlentities. $string=htmlentities($string); Any way around this? I don't want any other html code displayed. Quote Link to comment Share on other sites More sharing options...
afrojojo Posted June 23, 2010 Author Share Posted June 23, 2010 I think I got it. Adding the ENT_NOQUOTES seemed to do the trick. It was encoding the quotes and messing it up. $string=htmlentities($string,ENT_NOQUOTES); Thanks for all your help. I appreciate it. Quote Link to comment Share on other sites More sharing options...
salathe Posted June 23, 2010 Share Posted June 23, 2010 Glad you got it sorted. Next time please be sure to give accurate sample data/code, to help us to help you. Quote Link to comment 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.