chasethemetal Posted July 29, 2011 Share Posted July 29, 2011 Hey all,I am trying to figure out a way to search a string and return only the url in it. My string is an iframe so it looks something like this...<iframe src="http://www.site.com/something" width="400" height="200" frameborder="0"></iframe>And I'd like to search this and return only the URL. I guess the catch is that the URL will change in length and value so I need a function that will work dynamically.I was able to do it for a static URL, but I am having a difficult time coming up with a way to strip the iframe code out only leaving the URL left no matter how long or different the url becomes.Any tips or tricks would be appreciated. Thanks. Link to comment Share on other sites More sharing options...
chasethemetal Posted July 29, 2011 Author Share Posted July 29, 2011 Also let me explain what I'm trying to accomplish.I have iframes stored in a database. These iframes are actually vimeo videos. I am trying to rip the URL's out and then place them in another iframe of much smaller size, creating thumbnails of the video...So I figured if I could get the urls attached to a variable I could then loop them in a new iframe of much smaller width and height. Again the original iframe widths and heights change so I couldnt use preg_match to isolate because the patterns arent the same all the time.Agian, thanks any tips would be great. Link to comment Share on other sites More sharing options...
Ingolme Posted July 29, 2011 Share Posted July 29, 2011 You have to find a pattern that will extract the data you're looking for. As long as you're able to determine what part of it is what you need, so can a regular expression. Link to comment Share on other sites More sharing options...
chasethemetal Posted July 29, 2011 Author Share Posted July 29, 2011 Ok I am following you. I think I will find the pattern by looking for all the similar tag openings.. src, width.... and the assign $x = range(0,1000); that way the heights and widths will equal the range that they would and could fit in... I'll report back with code. Link to comment Share on other sites More sharing options...
chasethemetal Posted July 29, 2011 Author Share Posted July 29, 2011 This is where I am at. Maybe the foreach isn't the right approach... anyways if the range doesnt happen to end on width it does not work... makes sense because the end value is outside the loop... I am just trying to figure out a way to assign $number equal to an entire range at the same time. $iframe = '<iframe src="http://player.vimeo.com/video/00000000?title=0&byline=0&portrait=0" width="400" height="225" frameborder="0"></iframe>';foreach (range(398, 400) as $number) {$pattern[1] = "/<iframe src=\"/";$pattern[2] = "/\" width=\"".$number."\"/";$strip1 = preg_replace($pattern, $replace, $iframe);$replace[1] = "";$replace[2] = "";}echo $strip1; Link to comment Share on other sites More sharing options...
chasethemetal Posted July 29, 2011 Author Share Posted July 29, 2011 So this is working, except I know I am not using range() correctly... And can't see why really... Any help in getting $width and $height to equal a range from 0-1000 at the same time. $data = mysql_query("SELECT * FROM video ORDER BY id DESC") or die(mysql_error());while($info = mysql_fetch_array( $data )){ $file = $info['file']; $iframe = ''.$file.''; foreach (range(0, 400) as $width) { foreach (range(0, 225) as $height) {$pattern[1] = "/<iframe src=\"/";$pattern[2] = "/\" width=\"".$width."\"/";$pattern[3] = "/ height=\"".$height."\" frameborder=\"0\">/";$replace[1] = "";$replace[2] = "";$replace[3] = "";$strip1 = preg_replace($pattern, $replace, $iframe); } }echo '<iframe src="'.$strip1.'" width="200" height="150" frameborder="0"></iframe>'; }; Link to comment Share on other sites More sharing options...
chasethemetal Posted July 29, 2011 Author Share Posted July 29, 2011 Ok got it. Kind of. I just have to pray no one uses something smaller than 100 or larger than 1000. See any ways to accomplish this? $data = mysql_query("SELECT * FROM video ORDER BY id DESC") or die(mysql_error());while($info = mysql_fetch_array( $data )){ $file = $info['file'];$numStrip = preg_replace('/"[0-9][0-9][0-9]"/',"", "".$file.""); $iframe = ''.$numStrip.'';$pattern[1] = "/<iframe src=\"/";$pattern[2] = "/\" width=/";$pattern[3] = "/ height= frameborder=\"0\">/";$replace[1] = "";$replace[2] = "";$replace[3] = "";$url = preg_replace($pattern, $replace, $iframe);echo '<iframe src="'.$url.'" width="200" height="150" frameborder="0"></iframe>'; }; Link to comment Share on other sites More sharing options...
chasethemetal Posted July 29, 2011 Author Share Posted July 29, 2011 OK I GOT IT. I really just worked this one out live. If anyone is having this problem this should do it. If anyone sees anything that might be better let me know! $data = mysql_query("SELECT * FROM video ORDER BY id DESC") or die(mysql_error());while($info = mysql_fetch_array( $data )){ $file = $info['file'];$patternA[1] = '/"[0-9][0-9]"/';$patternA[2] = '/"[0-9][0-9][0-9]"/';$patternA[3] = '/"[0-9][0-9][0-9][0-9]"/';$numStrip = preg_replace($patternA,"", "".$file.""); $iframe = ''.$numStrip.'';$patternB[1] = "/<iframe src=\"/";$patternB[2] = "/\" width=/";$patternB[3] = "/ height= frameborder=\"0\">/";$url = preg_replace($patternB, "", $iframe);echo '<iframe src="'.$url.'" width="200" height="150" frameborder="0"></iframe>'; }; Link to comment Share on other sites More sharing options...
Ingolme Posted July 29, 2011 Share Posted July 29, 2011 I've got a better solution: $data = mysql_query("SELECT * FROM video ORDER BY id DESC") or die(mysql_error());while($info = mysql_fetch_array( $data )) { // Extract the data that's inside the src attribute preg_match('/<iframe src="([^"]+)"/i', $info['file'], $match); // Print the first parentheses match echo '<iframe src="' . $match[1] . '" width="200" height="150" frameborder="0"></iframe>';} Link to comment Share on other sites More sharing options...
chasethemetal Posted July 30, 2011 Author Share Posted July 30, 2011 Oh wow. That is a much more efficient method. Thank you. What is this called ([^"]+)"/i' I know it's a way to kind of define how the pattern is looked at or built right?I ended up changing it to...$pattern[1] = '/src="([^"]+)"/i';that way it would pick up most all video embed types such as youtube. If you could point me in the direction of some good info on how to do that would be great, this is something I must practice and learn. Thanks so much. Link to comment Share on other sites More sharing options...
Ingolme Posted July 30, 2011 Share Posted July 30, 2011 When used in the preg_match() function, things within parentheses are stored in the $match array. Their number indicates the order in which the opening parenthesis were. Match 0 returns the string that was captured using full expression.Some good references:http://regularexpressions.infohttp://php.net/preg_matchhttp://php.net/preg_match_all Link to comment Share on other sites More sharing options...
chasethemetal Posted July 30, 2011 Author Share Posted July 30, 2011 Thank you. Ill be reading up on my regex Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.