virtuexru Posted January 21, 2009 Share Posted January 21, 2009 Hey everyone . I have a script that puts an HTML page into a variable called $data. Basically, what I need to do now, is extract a certain part of the script into its own variables (array). There are six images that I need off the page to be put into separate variables. Here is the code for the images: <img src="http://somewebsite.com/website/website/pix/20081119/19667358_1.JPG?" width=96 height=72 border=0> <img src="http://somewebsite.com/website/website/pix/20081119/19667358_2.JPG?" width=96 height=72 border=0> <img src="http://somewebsite.com/website/website/pix/20081119/19667358_3.JPG?" width=96 height=72 border=0> Now, say I wanted to JUST extract the BOLDED part, how would I go about doing that? h**p://somewebsite.com/website/website/pix/20081119/19667358_3.JPG Or maybe just extracting the whole URL would be easier? Either one would work. Thanks in advance PHP Gurus!! Link to comment https://forums.phpfreaks.com/topic/141788-solved-help-extracting-certain-part-of-string/ Share on other sites More sharing options...
premiso Posted January 21, 2009 Share Posted January 21, 2009 It can be done with regex using preg_match_all. But since I am not good at that here is an alternative way. <?php $string = '<img src="http://somewebsite.com/website/website/pix/20081119/19667358_1.JPG?" width=96 height=72 border=0> <img src="http://somewebsite.com/website/website/pix/20081119/19667358_2.JPG?" width=96 height=72 border=0> <img src="http://somewebsite.com/website/website/pix/20081119/19667358_3.JPG?" width=96 height=72 border=0>'; $strings = explode("border=0>", $string); array_pop($strings); $urlData = array(); foreach ($strings as $line) { list($data) = explode('" width=', $line); list(,$data) = explode('src="', $data); $urlData[] = explode('/', $data); } print_r($urlData); ?> Should work. Untested. Using regex would take out alot of the guessing game. Maybe someone will show you that, which would lower this to a couple lines. Link to comment https://forums.phpfreaks.com/topic/141788-solved-help-extracting-certain-part-of-string/#findComment-742323 Share on other sites More sharing options...
virtuexru Posted January 21, 2009 Author Share Posted January 21, 2009 Figured it out: <?php $pattern = "/src=[\"]?([^\"]?.*(jpg))[\"]?/i"; preg_match_all($pattern, $data, $images); ?> Link to comment https://forums.phpfreaks.com/topic/141788-solved-help-extracting-certain-part-of-string/#findComment-742347 Share on other sites More sharing options...
premiso Posted January 21, 2009 Share Posted January 21, 2009 Figured it out: <?php $pattern = "/src=[\"]?([^\"]?.*(jpg))[\"]?/i"; preg_match_all($pattern, $data, $images); ?> Yep much nicer than my way =P Link to comment https://forums.phpfreaks.com/topic/141788-solved-help-extracting-certain-part-of-string/#findComment-742350 Share on other sites More sharing options...
virtuexru Posted January 21, 2009 Author Share Posted January 21, 2009 Figured it out: <?php $pattern = "/src=[\"]?([^\"]?.*(jpg))[\"]?/i"; preg_match_all($pattern, $data, $images); ?> Yep much nicer than my way =P Your way still worked, helped me figure out exactly what I needed. Thanks again! Link to comment https://forums.phpfreaks.com/topic/141788-solved-help-extracting-certain-part-of-string/#findComment-742371 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.