简体   繁体   中英

PHP how to Read URL and Modify the contents

I want to read a url, like Google, and modify the HTML tag value.

$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'/>

Then display the modified page.

How to do this in php, thank you very much

You would need to modify the content of $homepage variable. What file_get_contents does is store the content of the file as a string.

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.

Take a look into string functions here:

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

And particularly into this:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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