backyard Posted August 16, 2007 Share Posted August 16, 2007 This probably isn't hard but I can't seem to find the function I need. I have a string of say $items = 'xxxyyyvvvhttp://www.website.com/bbbb/id=1234zzzaaa'; x,y,v,z,b and a can change value and the number of instances of each isn't fixed. I want to pull the complete string of http://www.website.com/bbb/id=1234. The id=1234 and the http://www.website.com will never change. Anyone know what function I can use to pluck out the url thus leaving $items='http://www.website.com/bbb/id=1234'; Thanks Quote Link to comment https://forums.phpfreaks.com/topic/65330-solved-searching-a-string-for-url/ Share on other sites More sharing options...
lemmin Posted August 16, 2007 Share Posted August 16, 2007 $items = substr($string, strpos($string, "http://www.website.com/bbbb/id=1234"), 35); [/code 35 is the length of that string. Quote Link to comment https://forums.phpfreaks.com/topic/65330-solved-searching-a-string-for-url/#findComment-326266 Share on other sites More sharing options...
backyard Posted August 16, 2007 Author Share Posted August 16, 2007 Thanks for the response my problem is the values of b are not always the same number so I can't just count over 35 places. Quote Link to comment https://forums.phpfreaks.com/topic/65330-solved-searching-a-string-for-url/#findComment-326269 Share on other sites More sharing options...
Caesar Posted August 16, 2007 Share Posted August 16, 2007 <?php $string = 'xxxyyyvvvhttp://www.website.com/bbbb/id=1234zzzaaa'; preg_match('/[aA-zZ0-9]+http\:\/\/([aA-zZ.0-9\/\=]+id=1234)/i',$string,$url); echo $url[1]; //This is the url you want ?> Quote Link to comment https://forums.phpfreaks.com/topic/65330-solved-searching-a-string-for-url/#findComment-326270 Share on other sites More sharing options...
MadTechie Posted August 16, 2007 Share Posted August 16, 2007 <?php $data = 'xxxyyyvvvhttp://www.website.com/bbbb/id=1234zzzaaa'; preg_match_all('/((?:https?):\/\/(?:[a-z0-9]){3,3}?[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i', $data, $result, PREG_PATTERN_ORDER); $result = $result[1]; print_r($result) ?> works well for me for a know website use $data = 'xxxyyyvvvhttp://www.website.com/bbbb/id=1234zzzaaa xxxyyyvvvhttp://www.website.com/bbbb/id=1234zzzaaa '; preg_match_all('%(http://www\.website\.com/[\S]*)%i', $data, $result, PREG_PATTERN_ORDER); $result = $result[0]; print_r($result) Quote Link to comment https://forums.phpfreaks.com/topic/65330-solved-searching-a-string-for-url/#findComment-326271 Share on other sites More sharing options...
yarito Posted August 16, 2007 Share Posted August 16, 2007 Hi lemmin, not sure if it will work for you, but I use this function to extract urls from any text: // Convert www into links function convert_URLS($content) { $content = eregi_replace("((ht|f)tp://www\.|www\.)([a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})((/|\?)[a-z0-9~#%&\\/'_\+=:\?\.-]*)*)", "http://www.\\3", $content); $content = eregi_replace("((ht|f)tp://)((([a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3}))|(([0-9]{1,3}\.){3}([0-9]{1,3})))((/|\?)[a-z0-9~#%&'_\+=:\?\.-]*)*)", "<a href=\"\\0\" target=\"_blank\">\\0</a>", $content); return $content; } With a little work you should be able to get it to do what you want.. Quote Link to comment https://forums.phpfreaks.com/topic/65330-solved-searching-a-string-for-url/#findComment-326273 Share on other sites More sharing options...
Caesar Posted August 16, 2007 Share Posted August 16, 2007 Tested mine as well..though change to this to include the 'http' <?php $string = 'xxxyyyvvvhttp://www.website.com/bbbb/id=1234zzzaaa'; preg_match('/[aA-zZ0-9]+(http\:\/\/[aA-zZ.0-9\/\=]+id=1234)/i',$string,$url); print_r($url); ?> Quote Link to comment https://forums.phpfreaks.com/topic/65330-solved-searching-a-string-for-url/#findComment-326274 Share on other sites More sharing options...
d.shankar Posted August 17, 2007 Share Posted August 17, 2007 This works great !! FYI : I have changed your URL to show you that this code works ! <?php $string = "xxxyysdfsdfsdfsdfyvvvhttp://www.website.com/bbbbbbbbb/id=1234zzzaaa"; echo $string; echo "<br>"; $str1=str_replace("http://","^",$string); $pos1=strpos($str1,"^"); $str2=str_replace("com","*",$string); $pos2=strpos($str2,"*"); $str3=str_replace("id","^",$string); $pos3=strpos($str3,"^"); $str4=str_replace("1234","^",$string); $pos4=strpos($str4,"^"); $site1=substr($string,$pos1,$pos2-($pos1-4)); $site2=substr($string,$pos3,$pos4-($pos3-4)); $finalsite=$site1.$site2; echo $finalsite; ?> Quote Link to comment https://forums.phpfreaks.com/topic/65330-solved-searching-a-string-for-url/#findComment-326450 Share on other sites More sharing options...
backyard Posted August 22, 2007 Author Share Posted August 22, 2007 Actually some of the variables in the string had some special characters so Reply #4 worked best for me. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/65330-solved-searching-a-string-for-url/#findComment-330459 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.