Reputation: 2650
When I press 'View source code' of a certain web page, it's kind of like this:
<form action="/WANem/index-advanced.php" method="post">
<table border="1" width="100%">
<tr>
<td width="10%" >Delay time(ms) </td>
<td width="10%" ><input type="text" name="txtDelay1" size="7" value=1200>
</td>
<input type="submit" value="Apply settings" name="btnApply">
</table>
</form>
My question is: How can i get '1200' in the code using PHP. I mean i just want to get a certain string in the html code of another website without having to press 'view source code' and copy that string. Thanks for any reply.
Upvotes: 0
Views: 3138
Reputation: 12890
You can do this with file_get_contents() and preg_match().
//$url is whatever your URL is
$url = file_get_contents($url);
preg_match('|name="txtDelay1".*?value=([\d]+)|', $html, $html);
echo $html[1];
//should print your value 1200
Check out the regex here.
This is only going to work as long as this code appears exactly and is not duplicated. Also, if you are scraping from another site, it could be changed by the owner, and the regex would no longer work.
Upvotes: 0
Reputation: 13299
What you're trying to do is called "web scraping".
Here's a StackOverflow question with a bunch of helpful answers:
How to implement a web scraper in PHP?
And here is a tutorial that probably explains it better than I could by typing it out here:
http://www.thefutureoftheweb.com/blog/web-scrape-with-php-tutorial
Hope it helps and good luck!
Upvotes: 1
Reputation: 1122
In your php file it is something as simple as this:
$value = $_POST['txtDelay1'];
Although, it looks as a really basic question. I suggest you to go through some tutorials, to get the idea on how it all works.
First on in google php form tutorial
: http://www.phpf1.com/tutorial/php-form.html
Oh, now i see your edits. In that case, you can't skip sending a http request to get the source code, just like a browser does. Next, you have to parse the response from the server, just like a browser does as well. Ah, The response will be the "Source code" you're asking for. If you can, consider using python to this. It will be much more faster and efficient.
If PHP is a must, be aware that this task is a pain in the ass;)
Upvotes: 0