2017-07-19 154 views
0

我完全不熟悉php,而且我很難改變img標籤的src屬性。 我有一個網站,拉使用簡單的HTML DOM PHP頁面中的一部分,這裏是代碼:從img更改src屬性,使用簡單的DOM 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; 

?> 

此代碼正確返回我想要的頁面的一部分。但是,當我做到這一點的img標籤與原來的頁面的src:/assets/svg/icon_name.svg

,這樣它看起來像這是我想要做的就是改變原有的src:http://www.mywebsite.com/wp-content/themes/mytheme/assets/svg/icon_name.svg 我想把我的網站的網址放在assets/svg/icon_name.svg前面 我已經嘗試了一些教程,但是我無法做任何工作。

難道有人請幫助一個菜鳥在PHP?

回答

-1

沒有你閱讀文檔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; 
+0

嗨@zod,我看到那個頁面。但我在PHP語言上是個不錯的選擇。不知道如何使它工作。 – TDesign

+0

嗨@zod,非常感謝你,盡我所能地努力工作!謝謝... – TDesign

1

我可以使它發揮作用。所以如果有人有同樣的問題,這是我如何設法使代碼工作。

<?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; 

?> 

就是這樣!