Dec 26, 2012 07:57 AM
(This post was last modified: Dec 26, 2012 11:07 AM by Linux..)
Say for example I download the source code of a page, using cUrl.
Now say I want to find a line of that code only, but the problem is the value for it change all the time but it is always in SHA-1 form. I know what line it is located on so really I just need to get that line then I can remove the other values until I get the value I need by its self right? Sorry if I counfused anyone, this is similar to Regex in C# if you know anything about it.😵
Example code:
PHP Code
<?php $ch = curl_init(); $parms = array(); $settings = array( CURLOPT_URL=>'', CURLOPT_RETURNTRANSFER=>TRUE, CURLOPT_POST=>TRUE, CURLOPT_POSTFIELDS=>$parms, CURLOPT_VERBOSE=>TRUE );
curl_setopt_array($ch, $settings); $content = curl_exec($ch); curl_close($ch); //Downloaded the source code now, how do I search for the string? echo $content;
return true;
If your willing to change your code around abit, I've had better luck with DOM.
http://php.net/manual/en/book.dom.php
Have a look, it can find specific items with ease and less code is needed, I'm still learning about php so I don't know if this way would be faster.
http://simplehtmldom.sourceforge.net/
Should be fine if I understand your question right.
(Dec 26, 2012 08:34 AM)nibbles Wrote: If your willing to change your code around abit, I've had better luck with DOM.
http://php.net/manual/en/book.dom.php
Have a look, it can find specific items with ease and less code is needed, I'm still learning about php so I don't know if this way would be faster.
Can you give me an example? If so thank you so much !
Have a look at what CarrotMilk posted, looking at it it's pretty straight forward.
It's alot simpler than what's in the php manual...
PHP Code
<?php // Create DOM from URL or file $html = file_get_html('http://www.google.com/');
// Find all images foreach($html->find('img') as $element) echo $element->src . '<br>';
// Find all links foreach($html->find('a') as $element) echo $element->href . '<br>';
Dec 26, 2012 08:59 AM
(This post was last modified: Dec 26, 2012 09:00 AM by Linux..)
(Dec 26, 2012 08:49 AM)nibbles Wrote: Have a look at what CarrotMilk posted, looking at it it's pretty straight forward.
It's alot simpler than what's in the php manual...
PHP Code
<?php // Create DOM from URL or file $html = file_get_html('http://www.google.com/');
// Find all images foreach($html->find('img') as $element) echo $element->src . '<br>';
// Find all links foreach($html->find('a') as $element) echo $element->href . '<br>';
Can I search for tag names? Not like the tag id's but the names since the tag I want has no id only a name.
(Dec 26, 2012 08:45 AM)CarrotMilk Wrote: http://simplehtmldom.sourceforge.net/
Should be fine if I understand your question right.
Thank! I will take a moment and code it then post back see if anything could be simplifed and what not.
Looking at I would say something like this?
PHP Code
<?php foreach($html->find('div') as $element){
echo $element->class.'<br />';
}
If your looking for a match try this?
PHP Code
<?php foreach($html->find('div') as $element){ if($element->class === 'value') echo $element->class; else echo null; }
Edit: Just re-read your question, this might be what your looking for
PHP Code
<?php foreach($html->find('input') as $element) echo 'Value '.$element->value . ' Name: '. $element->name . '<br />';
I got a error with this code:
PHP Code
<?php $htmlDocument = file_get_html("http://twitter.com"); foreach($htmlDocument->find('authenticity_token') as $element) echo $element->value.'<br/>';
Says that "file_get_html" is a undefinded function.
PHP Code
<?php foreach($html->find('input') as $element){ if($element->name === 'authenticity_token') echo $element->value; else echo null; }
Give that a go :)
Dec 26, 2012 09:27 AM
(This post was last modified: Dec 26, 2012 09:27 AM by Linux..)
I downloaded it, and included the file here is the current code:
PHP Code
<?php include 'simple_html_dom.php'; // Create DOM from URL or file $html = file_get_html('http://www.twitter.com');
foreach($html->find('input') as $element){ if($element->name === 'authenticity_token') echo $element->value; else echo null; } ?>
Here is the new error:
Warning: file_get_contents( http://www.twitter.com): failed to open stream: Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP? in C:\Users\CENSORED\Desktop\xampp\htdocs\simple_html_dom.php on line 75
Fatal error: Call to a member function find() on a non-object in C:\Users\CENSORED\Desktop\xampp\htdocs\index.php on line 6
Dec 26, 2012 09:28 AM
(This post was last modified: Dec 26, 2012 09:28 AM by CarrotMilk.)
Try using cURL to grab the content of the page and save it to the $html var.
Go to your "php.ini" file and find "extension=php_openssl.dll" and remove the semicolon then restart your xampp server
DO NOT DEAL WITH THIS MEMBER
Dec 26, 2012 09:42 AM
(This post was last modified: Dec 26, 2012 09:43 AM by Yoshi-.)
You need to enable ssl first, but ssl is not even needed, the html_dom stuff is just written "badly".
Use a Regex, don't even bother parsing HTML is it pretty much pointless.
Something like this:
PHP Code
<?php $twit = file_get_contents("http://twitter.com");
preg_match("°\b[0-9a-f]{40}\b°", $twit, $match);
var_dump($match);
Or if you want to use curl
PHP Code
<?php $url = "http://twitter.com";
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $twit = curl_exec($ch); curl_close($ch);
preg_match("°\b[0-9a-f]{40}\b°", $twit, $match); var_dump($match);
"Don't even bother parsing HTML [..]"
wut whai. It's a terrible idea to parse HTML with RegEx.
DO NOT DEAL WITH THIS MEMBER
(Dec 26, 2012 09:47 AM)CarrotMilk Wrote: "Don't even bother parsing HTML [..]"
wut whai. It's a terrible idea to parse HTML with RegEx.
Reread what I said please. In this case Regex is the best thing you could use. He only wants one hash, running everything through a Dom parse, will only cause troubles, cause as soon as the HTML is not valid, it will go super mad.
You use DOM when you want to more than just fetching a hash, but in this case using regex is easily the best choice you have.
|