podja Posted July 2, 2007 Share Posted July 2, 2007 Hey, I am working on a script and I need to find something from some code. E.g. This code is inputted: <object width="425" height="350"><param name="movie" value=" name="wmode" value="transparent"></param><embed src=" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object> I want to locate http://www.youtube.com/v/ then find all the characters after that. So in this case it would return Zi_760pnGtg If you need it explaining more, just say so! Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/58114-find-url-in-code/ Share on other sites More sharing options...
john010117 Posted July 2, 2007 Share Posted July 2, 2007 Use strpos(). Quote Link to comment https://forums.phpfreaks.com/topic/58114-find-url-in-code/#findComment-288175 Share on other sites More sharing options...
podja Posted July 2, 2007 Author Share Posted July 2, 2007 Could you me an example of how this would work, because the script only knows to look for http://www.youtube.com/v/ - then it needs to find the characters after that to. Quote Link to comment https://forums.phpfreaks.com/topic/58114-find-url-in-code/#findComment-288188 Share on other sites More sharing options...
kathas Posted July 2, 2007 Share Posted July 2, 2007 Since you know for sure that the first part is going to be youtube.com/v/ you can use of course regex but you can also use the faster alternative provided below... <?php $text = //the text you want to search... $matches = array(); // will hold the matched random chars $i = strpos($text,'youtube.com/v/'); $length = strlen('youtube.com/v/'); while ( $i !== FALSE) { $l = strpos($text,'"',$i); if ($l !== FALSE) { $matches[] = substr($text,$i+$length,$l-$i-$length); } $i = strpos($text,'youtube.com/v/',$l); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/58114-find-url-in-code/#findComment-288197 Share on other sites More sharing options...
corillo181 Posted July 2, 2007 Share Posted July 2, 2007 if find that but only return the rest of the url so it will find the link your posted, but only retrieve what is after it., Quote Link to comment https://forums.phpfreaks.com/topic/58114-find-url-in-code/#findComment-288201 Share on other sites More sharing options...
kathas Posted July 2, 2007 Share Posted July 2, 2007 if that was an answer to me please edit your post so i can understand the problem... if not disregard this message... Quote Link to comment https://forums.phpfreaks.com/topic/58114-find-url-in-code/#findComment-288208 Share on other sites More sharing options...
podja Posted July 2, 2007 Author Share Posted July 2, 2007 That worked kathas, its just it ends up with a slash on the end. And ideas how to get rid of that? My code is: <?php if(isset($_POST['submit'])) { $input = $_POST['input']; $matches = array(); // will hold the matched random chars $i = strpos($input,'youtube.com/v/'); $length = strlen('youtube.com/v/'); while ( $i !== FALSE) { $l = strpos($input,'"',$i); if ($l !== FALSE) { $matches[] = substr($input,$i+$length,$l-$i-$length); } $i = strpos($input,'youtube.com/v/',$l); } echo "<br>"; echo $matches[1]; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/58114-find-url-in-code/#findComment-288212 Share on other sites More sharing options...
kathas Posted July 2, 2007 Share Posted July 2, 2007 well the i suppose that there is a slash after the random chars in the link provided you can get rid of that many ways i would suggest the following change... <?php if(isset($_POST['submit'])) { $input = $_POST['input']; $matches = array(); // will hold the matched random chars $i = strpos($input,'youtube.com/v/'); $length = strlen('youtube.com/v/'); while ( $i !== FALSE) { $l = strpos($input,'"',$i); if ($l !== FALSE) { $matches[] = trim(substr($input,$i+$length,$l-$i-$length),'/'); } $i = strpos($input,'youtube.com/v/',$l); } echo "<br>"; echo $matches[1]; } ?> now using the trim the $matches should only contain the random chars either if the provided text has a slash after them or if it doesn't... Kathas Edit: i would print_r() the $matches array to see if it really works... Quote Link to comment https://forums.phpfreaks.com/topic/58114-find-url-in-code/#findComment-288221 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.