2017-08-08 66 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是的,谢谢你,先生它完美的作品 –