使用 Simple HTML Dom php 库从 img 更改 src 属性

Change src atribute from img, using Simple HTML Dom php library

我是 php 的新手,我很难更改 img 标签的 src 属性。 我有一个网站使用 Simple Html Dom php 拉取页面的一部分,这里是代码:

<?php

include_once('simple_html_dom.php');

$html = file_get_html('http://www.tabuademares.com/br/bahia/morro-de-sao-paulo');

foreach($html ->find('img') as $item) {
    $item->outertext = '';
    }

$html->save();

$elem = $html->find('table[id=tabla_mareas]', 0);
echo $elem;

?>

此代码正确 returns 我想要的页面部分。但是当我这样做时,img 标签带有原始页面的 src:/assets/svg/icon_name.svg

我想做的是把原来的src改成这样:http://www.mywebsite.com/wp-content/themes/mytheme/assets/svg/icon_name.svg 我想把我网站的 url 放在 assets / svg / icon_name.svg 前面 我已经尝试了一些教程,但我无法完成任何工作。

有人可以帮助 php 中的菜鸟吗?

你读过文档了吗for read and modify attributes 按照那个

// Get a attribute ( If the attribute is non-value attribute (eg. checked, selected...), it will returns true or false)
$value = $e->href;

// Set a attribute
$e->href = 'ursitename'.$value;

我可以让它工作。所以如果有人有同样的问题,这里是我如何让代码工作的。

<?php

// Note you must download the php files simple_html_dom.php from 
// this link https://sourceforge.net/projects/simplehtmldom/files/

//than include them
include_once('simple_html_dom.php');

//target the website
$html = file_get_html('http://the_target_website.com');

//loop thru all images of the html dom
foreach($html ->find('img') as $item) {

// Get a attribute ( If the attribute is non-value attribute (eg. checked, selected...), it will returns true or false)

        $value = $item->src;

        // Set a attribute

        $item->src = 'http://yourwebsite.com/'.$value;
    }
//save the variable
$html->save();

//findo on html the div you want to get the content
$elem = $html->find('div[id=container]', 0);

//output it using echo
echo $elem;

?>

就是这样!