Jump to content

[SOLVED] Help extracting certain part of string..


virtuexru

Recommended Posts

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!!

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.