简体   繁体   English

PHP如何读取URL和修改内容

[英]PHP how to Read URL and Modify the contents

I want to read a url, like Google, and modify the HTML tag value. 我想读取一个网址,例如Google,并修改HTML标记值。

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

For example, after read google link, I want set the google search box value to hello word , something like <input type='text' value='hello world'/> 例如,在读取google链接之后,我要将google搜索框的值设置为hello word ,例如<input type='text' value='hello world'/>

Then display the modified page. 然后显示修改后的页面。

How to do this in php, thank you very much 如何在php中做到这一点,非常感谢

You would need to modify the content of $homepage variable. 您将需要修改$homepage变量的内容。 What file_get_contents does is store the content of the file as a string. file_get_contents作用是将文件的内容存储为字符串。

So you would have to look up the textbox identifier and use a string function to add the value to the textbox. 因此,您必须查找文本框标识符,并使用字符串函数将值添加到文本框。

Afterwards you would echo the modified $homepage variable. 之后,您将回显修改后的$homepage变量。

Take a look into string functions here: 在这里查看字符串函数:

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

And particularly into this: 特别是:

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

If you want to see the string of code that you are actually working with put this at the beginning of your file: 如果要查看实际使用的代码字符串,请将其放在文件开头:

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

echo $homepage;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM