PHP如何读取URL和修改内容


PHP how to Read URL and Modify the contents

我想读取一个网址,比如谷歌,并修改HTML标签值。

$homepage = file_get_contents('http://www.google.com');
echo $homepage;

例如,阅读谷歌链接后,我想将谷歌搜索框值设置为 hello word,类似<input type='text' value='hello world'/>

然后显示修改后的页面。

如何在php中执行此操作,非常感谢

您需要

修改$homepage变量的内容。file_get_contents的作用是将文件的内容存储为字符串。

因此,您必须查找文本框标识符并使用字符串函数将值添加到文本框中。

之后,您将回显修改后的$homepage变量。

在这里看看字符串函数:

http://www.w3schools.com/php/php_ref_string.asp

特别是这个:

http://www.w3schools.com/php/func_string_substr_replace.asp

如果你想看到你实际使用的代码字符串,把它放在文件的开头:

header('Content-type: text/plain');
$homepage = file_get_contents('http://www.google.com');
echo $homepage;