2009-08-01 120 views
0

我正在使用file_get_contents抓取一個頁面,但我需要在回顯頁面內容之前替換一些數據。使用str_replace和file_get_contents

我有這個迄今爲止(這個腳本在運行domain2.com)

<?php 
$page = file_get_contents('http://domain.com/page.html'); 
str_replace('href="/','href="http://domain.com','$page'); 
echo $page; 
?> 

的問題是,當頁面顯示,該domain.com頁面上的一些鏈接閱讀:

<a href=/about.html> 

當我在我的腳本中調用時,正在將它添加到不正確的域。我試着用str_replace函數,尋找

href="/ 

href="http://www.domain.com/ 

但它不工作更換。任何線索?

+0

我刪除了我的答案,因爲如果執行會有一噸與它的問題。 – 2009-08-01 05:58:29

回答

3

固定它

$pagefixed = str_replace("href=\"/","href=\"http://www.domain.com/","$page"); 

感謝所有

2

由於引號不同,您將需要使用正則表達式(preg_replace)或2 str_replaces。