Php 从字符串中获取图像上载它们并替换为新链接
我有一个字符串,其中包含文本和照片,如下面所示。 到目前为止,我的代码获取所有图像并将它们上传到一个文件夹中。 我需要用正确的Order替换新上传的链接Php 从字符串中获取图像上载它们并替换为新链接,php,regex,dom,preg-replace,Php,Regex,Dom,Preg Replace,我有一个字符串,其中包含文本和照片,如下面所示。 到目前为止,我的代码获取所有图像并将它们上传到一个文件夹中。 我需要用正确的Order替换新上传的链接 $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 y
$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
}
}
$nextstep=“您好,这是图像1!!现在您可以看到图像2”;
$string=$nextstep;
$doc=新的DOMDocument();
$doc->loadHTML($string);
$images=$doc->getElementsByTagName('img');
foreach($images作为$image){//开始循环
回声“”;
echo$image->getAttribute('src')。“\n”;
回声“”;
$urlimg=$image->getAttribute('src');//图像URL
$URL=urldecode($urlimg);
$image_name=(stristr($URL',?',true))?stristr($URL',?',true):$URL;
$pos=strrpos($image_name,'/');
$image\u name=substr($image\u name,$pos+1);
$extension=stristr($image_name,'.');
如果($extension='.jpg'| |$extension='.png'| |$extension='.gif'| |$extension=='.jpeg'){
$img='../images/'.$image\u name;
file_put_contents($img,file_get_contents($url));//逐个上传
}
}
现在还不清楚期望的结果是什么。听起来您想将现有字符串中的src
URL更改为保存图像的URL。如果情况并非如此,请尝试更新问题以使其更清晰
这里有一个简单的方法来解决这个问题
步骤1-使用源字符串从DOM中提取img标记
步骤3-将img src URL修改为新路径
正确的顺序是什么?如何定义?例如“您好,您可以看到花(花的图像),现在您可以看到树(树的图像),因此被替换的URL必须是第一个花,第二个树,但是当您阅读它们时,您如何知道什么是第一个与第二个/第三个/等等…或者这是一次性运行?此$imgURLs=[];标记为错误。您知道原因吗?可能是因为您使用的是不受支持的版本。从PHP 5.4.0开始就存在,如果您使用的是比该版本旧的版本,则已过时。有关详细信息,请参阅,以及不再支持哪些版本的PHP。图像已上载,但以新的echo$dom->saveHTML()格式;它显示的是旧的url。你认为问题是速记数组被高位化是一个错误吗?有什么兼容的方法吗?非常感谢你的时间,先生。我使用的是PHP5.5.3如果你使用的是PHP5.5.3,你不会因此得到语法错误。也许你会分享实际的错误?可能是其他原因?我发现所有的image SRC保留原始url并添加../images/upload image.jpg,因此我需要删除此原始url
$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');
}
$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));
}
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.