Newsgroup: it.comp.www.php
Titolo: grabbare una pagina html (con le immagini)

non grabba le immagini con gli spazi
function imageGrabber($url, $unique = 1)
{
    $startTag = '<img';
    $srcTag = 'src=';
    $endTag = '>';
    $counter = 0;
    if(!is_array($url))
    {
        $url = array($url);
    }
    if ($unique !== 0 && $unique !== 1)
    {
        printf('Invalid parameter for $unique. The parameter must be either
1 or 0.');
        exit();
    }
    foreach ($url as $value)
    {
        $contents = file_get_contents($value);
        $domain = $value;
        $domain = substr($domain, 7);
        $pos = stripos($domain, '/');
        if ($pos)
        {
            $domain = substr($domain, 0, stripos($domain, '/'));
        }
        while ($contents)
        {
            set_time_limit(0);                                    # In case
we have several large pages
            $quotes = array('"', "'", "\n");
            $contents = str_replace($quotes, '', $contents);    # Strip "
and ' as well as \n from input string
            $contents = stristr($contents, $startTag);            # Drop
everything before the start tag '<img'
            $contents = stristr($contents, $srcTag);            # Drop
everything before the 'src'
            $endTagPosition = stripos($contents, $endTag);        # Position
of the end tag '>'
            $src = substr($contents, 4, $endTagPosition - 4);    # Get
everything from src to end tag --> 'src="path" something>'
            $spacePosition = stripos($src, ' ');                # Position
of space (if it exists)
            if ($spacePosition !== false)
            {
                $src = substr($src, 0, $spacePosition);            # Drop
everything after space, keeping 'src="path"'
            }
            $questionMarkPosition = stripos($src, '?');
            if ($questionMarkPosition !== false)
            {
                $src = substr($src, 0, $questionMarkPosition);    # Remove
any part after a '?'
            }
            $contents = stristr($contents, $endTag);            # Drop
everything before the end tag '>'
            if ($src)
            {
                if (stripos($src, '/') === 0)
                {
                    $src = 'http://'.$domain.$src;                # Relative
link, so add domain before '/'
                }
                else
                {
                    if (stripos($src, 'http://') !== 0 && stripos($src,
'https://') !== 0 && stripos($src, 'ftp://') !== 0)
                    {
                        $src = 'http://'.$domain.'/'.$src;        # Relative
link, so add domain and '/'
                    }
                }
                $paths[] = $src;
            }
        }
        if ($unique === 1)
        {
            $results[] = array_unique($paths);        # Create final array
with unique $paths
        }
        else
        {
            $results[] = $paths;                    # Create final array
with all $paths
        }
        $paths = array();                            # Reset links
        $counter++;                                    # Increment counter
    }
    return $results;
}
/**
* This function will downlaod and save all images on the specified
directory.
*
* @param array $results
* @param sting $localPath
*/
function saveImages($results, $localPath = './c')
{       
    foreach ($results as $v)
    {           
        foreach ($v as $value)
        {
            set_time_limit(0);
 
            $path = $value;
 
            if (!file_exists($localPath))
            {
                mkdir($localPath);                    # Create the dir if it doesn't exist
            }
 
            $localFile = $localPath.basename($path);
 
            if (!copy($path, $localFile))
            {
                   echo "<font color=red>Failed to copy $path</font><br>";
            }
            else
            {
                echo "<font color=blue>Successfully copied
$path</font><br>";
            }
        }
    }
}
?>
<html>
<head>
</head>
<body>
    <?php
        $array = array('
http://127.0.0.1/html_parser-2003-12-14/test.html'
);
        $links = imageGrabber($array);
        saveImages($links);
    ?>
</body>
</html>

 non grabba le immagini con gli spazi
E allora?
-- 
I am what I am, I do what I can
Do. Or do not. There is not try.