更改从XML生成文件的参数(url和image url)

时间:2014-11-19 07:57:34

标签: php xml url

我有一个我在我的网站上使用的xml RSS Feed,这段代码我从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.jpghttp://mywebsite//img/booktitle.jpg

每次网址结构都相同:

http://books.com/new_books/booktitle/

http://books.com/new_books/something/

http://books.com/new_books/else/

我网站上的Stricture是一样的:

http://mywebsite.com/new_books/booktitle/

http://mywebsite.com/new_books/something/

http://mywebsite.com/new_books/else/

同样的$ img,所以我唯一要改变的是books.com到mywebsite.com

1 个答案:

答案 0 :(得分:0)

我就这样做了:

$link = str_replace("books.com","mywebsite.com",$link);

之后添加:

$link = $xml->resource[$i]->book_link;