PHP更改图像位置

时间:2016-04-25 14:59:17

标签: php image content-management-system

我使用的是旧的内容管理系统,它使用CKEditor和CKFinder,允许您将图像上传到服务器,但是它将完整的URL放入内容框中,稍后我将其保存到数据库中。

我最近重建了我的内容管理系统,并再次使用CKEditor和CKFinder。我目前正在移动数据库并重新排列数据。但我注意到这些图像的URL是错误的。

所以我的问题是,是否可以搜索和替换URL,每个字符串可能多次?中间还有一个随机的哈希键,我不太清楚如何绕过,这就是我提出这个问题的原因。

网址为(未经编辑)

http://www.oldwebsite.com/aknsnd8123nakndkna/upload/images/image.jpg

我想提取网址并将其路径更改为:

http://www.newwebsite.com/images/uploaded/image.jpg

我不确定如何开始和结束,同时跳过中间部分。或者是否可以在最后获得image.jpg,我将在开头放入新的路径。

到目前为止,我只有2行代码,但要记住它是HTML。

$content = '<h1>'.$page['Page_Title'].'</h1>';
$content .= $page['Page_Content'];  

这是$page['Page_Content']返回的内容:

<p style="text-align: center;">
    <img alt="" src="http://www.oldwebsite.com/aknsnd8123nakndkna/upload/images/image.jpg" style="width: 700px; height: 493px;" />
</p>
<p style="text-align: center;">
    The Sports Stadium is an impressive addition to any event, catering for several sports keeping children entertained for hours.
</p>

2 个答案:

答案 0 :(得分:1)

我不确定你的表结构是什么。您可以在旧数据库上运行如下所示的mysql查询来更新路径。如果这对您没有帮助,请发布您的表格结构,我很乐意更新我的答案。

sql=" UPDATE page SET page_content = REPLACE(page_content, 'http://www.oldwebsite.com/aknsnd8123nakndkna/upload/images/', 'http://www.newwebsite.com/images/uploaded/')";

希望得到这个帮助。

答案 1 :(得分:0)

我找到了怎么做!感谢ÁlvaroGonzález提出的建议!

    $content = '<h1>'.$page['Page_Title'].'</h1>';
    $content .= $page['Page_Content'];  
    $html = new DOMDocument(); 
    $html->loadHTML($content);  
    foreach ($html->getElementsByTagName('img') as $image) {
        $img = array_reverse(explode('/', $image->getAttribute('src')));
        $image->setAttribute('src', $website.'/images/uploaded/'.$img[0]);
    }
    $content = $html->saveHTML();