我有一个 xml rss 提要,我正在我的网站上使用,我用这段代码从 xml 文件生成 html:
$html = "";
$url = "http://books.com/new_bookss/?format=xml";
$xml = simplexml_load_file($url);
for($i = 0; $i < 10; $i++){
$link = $xml->resource[$i]->book_link;
$title = $xml->resource[$i]->book_title;
$img = $xml->resource[$i]->image_url;
$html .= "<a href='"$link'"><img src='"$img'"><br>$title</a>";
}
echo $html;
生成的$link和$img如下所示:
http://books.com/new_books/booktitle/ /*this is for $link*/
http://images.books.com/img/booktitle.jpg /* this is for $img*/
我必须以这种方式更改这些网址:
http://books.com/new_books/booktitle/
到http://mywebsite/new_books/booktitle/
http://images.books.com/img/booktitle.jpg
到http://mywebsite//img/booktitle.jpg
网址结构每次看起来都一样:
http://books.com/new_books/booktitle/
http://books.com/new_books/something/
http://books.com/new_books/else/
我网站上的限制是相同的:
http://mywebsite.com/new_books/booktitle/
http://mywebsite.com/new_books/something/
http://mywebsite.com/new_books/else/
$img也一样,所以我唯一需要改变的就是 books.com mywebsite.com
我就是这样做的:
$link = str_replace("books.com","mywebsite.com",$link);
添加后:
$link = $xml->resource[$i]->book_link;