【问题标题】:get images from an string upload them and replace with new links从字符串中获取图像上传它们并用新链接替换
【发布时间】:2015-10-10 21:36:13
【问题描述】:

我有一个包含文本和照片的字符串,如下所示。 到目前为止,我的代码获取了所有图像并将它们上传到一个文件夹中。 我需要用正确的顺序替换新上传的链接。

$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
    }
}

【问题讨论】:

  • 正确的顺序是什么?如何定义?
  • fo 例如“你好,你可以看到花(花的图像),现在你可以看到树(树的图像)所以替换的 url 必须首先是花,然后是树
  • 但是什么时候阅读它们,你怎么知道什么是第一和第二/第三/等等......或者这是一次运行?

标签: php regex dom preg-replace


【解决方案1】:

目前尚不清楚这里期望的结果是什么。听起来您想将现有字符串中的src URL 更改为您保存图像的那个。如果不是这种情况,请尝试更新问题以获得更清晰的信息。

这是解决问题的简单方法...

第 1 步 - 使用源字符串从 DOM 中提取 img 标签

$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');
}

第 2 步 - 将图像保存在其他位置

$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));
}

第 3 步 - 将 img src URL 修改为新路径

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.

【讨论】:

  • 这个 $imgURLs = [];被标记为错误。你知道为什么吗?
  • 可能是因为您使用的是不受支持的版本。 Short-hand array syntax 自 PHP 5.4.0 以来一直存在,如果您使用的任何旧版本都已过时。有关更多信息以及不再支持的 PHP 版本,请参阅 PHP EOL reference
  • 图片已上传,但在新的 echo $dom->saveHTML();它显示旧网址。您是否认为问题在于 Short hand 数组被突出显示为错误?有什么兼容的方法吗?非常感谢您抽出宝贵的时间,先生。我正在使用 php 5.5.3
  • 如果您使用的是 PHP 5.5.3,则不会出现语法错误。也许分享实际的错误?可能是别的什么?
  • 我发现所有图片来源都保留了原始 url 并添加了 ../images/uploaded image 。 jpg 所以我需要删除这个原始网址
猜你喜欢
  • 1970-01-01
  • 2021-11-03
  • 2013-05-06
  • 1970-01-01
  • 2018-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多