Jump to content

How to extract a string between "http://" and ".jpg"


aarongoos

Recommended Posts

Is there any way to do this? If you want to extract the URLs for all the pictures on a given page, it would be easy to match "http://" and ".jpg" and extract anything in between those two strings.

I haven't been able to find a function that can extract an unknown length between two elements. Does it exist?
Something like this may work...

[code]<?php

// the data container that holds the HTML document
// you want to extract image URL(s) from...

$in = '';

$out = array ();

preg_match_all ( "|src\=\"?'?`?([[:alnum:]:?=&@/._-]+)\"?'?`?|i", $in, $src );

for ( $x = 0; $x < sizeof ( $src[1] ); $x++ )
{
// we don't want copies on the same image url

if ( ! in_array ( $src[1][$x], $out ) )
{
$out[] = $src[1][$x];
}
}

print_r ( $out );

?>[/code]


printf

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.