Jump to content

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.

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.