问题描述:

I have a string that contains text and photos as you can see bellow.

My code so far get all the images and upload them into a folder.

I need to replace the new uploaded links with the correct oreder.

$nextstep = "Hello there this is image 1 <img src='http://www.demosite.com/wp-content/uploads/2015/01.jpg' width='653' height='340' alt='xxx' title='xxx'> !! And Now you can see image number 2 <img src='http://www.demosite.com/wp-content/uploads/2015/02.jpg' width='653' height='340' alt='xxx' title='xxx'>";

$string = $nextstep;

$doc = new DOMDocument();

$doc->loadHTML($string);

$images = $doc->getElementsByTagName('img');

foreach ($images as $image) { //STARTING LOOP

echo "</br>";

echo $image->getAttribute('src') . "\n";

echo "</br>";

$urlimg = $image->getAttribute('src'); //IMAGE URL

$URL = urldecode($urlimg);

$image_name = (stristr($URL,'?',true))?stristr($URL,'?',true):$URL;

$pos = strrpos($image_name,'/');

$image_name = substr($image_name,$pos+1);

$extension = stristr($image_name,'.');

if($extension == '.jpg' || $extension == '.png' || $extension == '.gif' || $extension == '.jpeg'){

$img = '../images/' . $image_name;

file_put_contents($img, file_get_contents($url)); //UPLOAD THEM ONE BY ONE

}

}

网友答案:

It's not clear what the desired outcome is here. It sounds like you want to change the src URL in your existing string to the one where you've saved the images. If this isn't the case please do try updating the question for more clarity.

Here's a simple way to break down the problem...

Step 1 - Extract the img tags from DOM using source string

$html = <<<'HTML'
Hello there this is image 1 <img src="http://www.demosite.com/wp-content/uploads/2015/01.jpg" width="653" height="340" alt="xxx" title="xxx"> !! 

And Now you can see image number 2 <img src="http://www.demosite.com/wp-content/uploads/2015/02.jpg" width="653" height="340" alt="xxx" title="xxx">
HTML;

$dom = new DOMDocument;
$dom->loadHTML($html);
$imgs = $dom->getElementsByTagName('img');

// Store the list of image urls in an array - this will come in handy later
$imgURLs = [];
foreach($imgs as $img) {
    if (!$img->hasAttribute('src')) {
        continue;
    }
    $imgURLs[] = $img->getAttribute('src');
}

Step 2 - Save the image in a different location

$newImgURLs = [];          // new modified URLs where images were moved
$newPath    = '../images'; // wherever you're saving the images
foreach($imgURLs as $imgURL) {
    /**
     *  Use parse_url and pathinfo to break down the URL parts and extract the
     *  filename/extension instead of the fragile implementation you used above
     */
    $URLparts     = parse_url($imgURL);
    $file         = pathinfo($URLparts['path']);
    $fileName     = $file['filename'] . '.' . $file['extension'];
    $newFileName  = $newPath . '/' . $fileName;
    $newImgURLs[] = $URLparts['scheme'] . '://' .
                    $URLparts['host'] . $file['dirname'] . '/' . $newFileName .
                    (isset($URLparts['query']) ? ('?' . $URLparts['query']) : null) .
                    (isset($URLparts['fragment']) ? ('#' . $URLparts['fragment']) : null);
    // download image and save to new location
    file_put_contents($newFileName, file_get_contents($imgURL));
}

Step 3 - Modify the img src URLs to new path

foreach($imgs as $i => $img) {
    $img->setAttribute('src', $newImgURLs[$i])
}
echo $dom->saveHTML(); // new updated DOM
// or just create a new $html string from scratch using the new URLs.
相关阅读:
Top