2017-08-08 35 views
1

我使用的file_get_contents在我的網站 顯示其他網站,我想這裏面的內容全部鏈接是這樣的:更改所有鏈接的file_get_contents內PHP

<a href="www.google.com/about.us"></a> 

不喜歡這樣的:

<a href="/about.us"></a> 
+2

爲什麼你顯示其他人的網站?我曾經有人在我的網站上做過這件事,我真的很討厭它! – Jerodev

+0

比什麼是問題? – MKR

+0

我想在線代理 –

回答

0

我會在輸出字符串上使用str_replace()。例如,

$newOutput = str_replace('<a href="/', '<a href="www.google.com/', $output); 

哪裏$output是從file_get_contents()數據。例如,

$output = file_get_contents('example.txt'); 

編輯:

爲了配合新的要求,您在評論中爲了做到這一點只在身體的聯繫,那麼你應該將$output分成兩個部分提到,頭部和身體,然後只在第二個這樣做。因此,你應該有的代碼是:

$output = file_get_contents('example.txt'); //get all content 
$outputArray = explode('</head>', $output); //split the content 
$outputBody = $outputArray[1]; //get everything after the </head> 
$newOutput = str_replace('<a href="/', '<a href="www.google.com/', $outputBody); 
+0

感謝您的回答 這是工作只有在所有相關鏈接 但file_get_content有一些相關的鏈接和一些絕對鏈接 –

+0

正如你可以看到,這將不會觸及絕對鏈接,因爲它必須匹配第一個字符串'

+1

ooohh是的,謝謝你,先生它完美的作品 –