【发布时间】: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