How to use value from file_get_contents php as a number ?
I want to use $val in this case = 50.0001 plus with 20 it's will be result 70.0001 But when i test it's show 0 why ? how can i do ?
<?php
$html = file_get_contents('https://www.example.com');
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html);
$finder = new DomXPath($doc);
$node = $finder->query("//*[contains(#class, 'test')]");
$val = $doc->saveHTML($node->item(0));
$result = $val + 20;
echo $result;
?>
and https://www.example.com
<span class="test">50.0001</span>
just get the textcontent from the node and don't use saveHTML.
$val = $node->item(0)->textContent;
Related
I've this HTMl coming from a file_get_contents:
<div class="attractions-attraction-filtered-common-ListingsHeader__listingsCount--PflJ1">
<span>We found <b>10 results</b> for you.</span>
</div>
How can I get the number of results (i.e.: 10)?
Note, that the part PflJ1 is something random.
This is what I tried:
$page = file_get_contents($url);
$dom = new DOMDocument();
$dom->loadHTML($html);
$xp = new DOMXpath($dom);
$activitiesNb = $xp->query('//div/span/text()');
$activitiesNb = $activitiesNb->nodeValue;
echo $activitiesNb;
But it does not work.
What I'm missing please ?
Thanks.
Using evaluate():
$results = $xp->evaluate('string(//span[contains(., "We found ")]/b/text())');
Your XPath needs to be fixed (you're looking for the content of a b element). Also, use item() in combination with nodevalue.
<?php
$html = <<<'HTML'
<div class="attractions-attraction-filtered-common-ListingsHeader__listingsCount--PflJ1">
<span>We found <b>10 results</b> for you.</span>
</div>
HTML;
$document = new DOMDocument();
$document->loadHTML($html);
$xpath = new DOMXpath($document);
$res = $xpath->query('//div[starts-with(#class,"attractions-")]/span/b');
$val = $res ->item(0)->nodeValue;
echo substr($val,0,2)
?>
Output : 10
Alternative :
$res = $xpath->evaluate("substring(//div[starts-with(#class,'attractions-')]/span/b,1,2)");
echo $res
And if you just have one number (for e.g. 8) :
echo substr($val,0,1)
or
$res = $xpath->evaluate("substring(//div[starts-with(#class,'attractions-')]/span/b,1,1)");
echo $res
I am receiving the following xml file:
<FEEDBACKLIST>
<SUMMARY>
<MODE>service</MODE>
<VENDORLOGON/>
<VENDORREF/>
<TOTALSERVICECOUNT>0</TOTALSERVICECOUNT>
<TOTALPRODUCTCOUNT>0</TOTALPRODUCTCOUNT>
<COUNT>0</COUNT>
<TITLE/>
<BEST>100</BEST>
<WORST>0</WORST>
<AVERAGE>??</AVERAGE>
<START>1</START>
<LIMIT>20</LIMIT>
</SUMMARY>
</FEEDBACKLIST>
Can I check if the COUNT is 0 and if it is I would like to resend the xml request with different parameters.
I am receiving the it this way:
$curdir = getcwd();
$logon = array_key_exists('logon', $_GET) ? $_GET['logon'] : null;
$limit = array_key_exists('limit', $_GET) ? $_GET['limit'] : null;
$mode = array_key_exists('mode', $_GET) ? $_GET['mode'] : null;
$vendorref = array_key_exists('vendorref', $_GET) ? $_GET['vendorref'] : null;
$suppressnegatives = array_key_exists('suppressnegatives', $_GET) ? $_GET['suppressnegatives'] : null;
$xml_filename = "http://www.x.com/filename/xmlfeed.jsp?logon=".$logon;
if ($limit)
$xml_filename .= "&limit=".$limit;
if ($vendorref)
$xml_filename.="&vendorref=".$vendorref;
if ($mode)
$xml_filename.="&mode=".$mode;
if ($suppressnegatives)
$xml_filename.="&negativesanswered=true";
if (phpversion() < "5"){
$xmldoc = domxml_open_file( $xml_filename);
$xsldoc = domxml_xslt_stylesheet_file ( $curdir."/feedback.xsl");
$result = $xsldoc->process($xmldoc);
echo $result->dump_mem();
}
else
$doc = new DOMDocument();
$xsl = new XSLTProcessor();
$doc->load($curdir."/feedback.xsl");
$xsl->importStyleSheet($doc);
$doc->load($xml_filename);
echo $xsl->transformToXML($doc);
I think I need to check before it echo's out the $xsl with getElementsbyTagName but not sure on how to use this.
Any help welcome
If you need to keep the PHP 4 support, have fun traversing the DOM. If you can remove it and support only PHP 5 use the DOMXpath::evaluate().
$xml = '<FEEDBACKLIST><SUMMARY><COUNT>0</COUNT></SUMMARY></FEEDBACKLIST>';
$dom = new DOMDOcument();
$dom->loadXml($xml);
$xpath = new DOMXpath($dom);
var_dump($xpath->evaluate('string(/FEEDBACKLIST/SUMMARY/COUNT)'));
Output:
string(1) "0"
I'm working with a DOM parser and I'm having issues. I'm basically trying to grab the href within the tag that only contain the class ID of 'thumbnail '. I've been trying to print the links on the screen and still get no results. Any help is appreciated. I also turned on error_reporting(E_ALL); and still nothing.
$html = file_get_contents('http://www.reddit.com/r/funny');
$dom = new DOMDocument();
#$dom->loadHTML($html);
$classId = "thumbnail ";
$div = $html->find('a#'.$classId);
echo $div;
I also tried this but still had the same result of NOTHING:
include('simple_html_dom.php');
$html = file_get_contents('http://www.reddit.com/r/funny');
$dom = new DOMDocument();
#$dom->loadHTML($html);
// grab all the on the page
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");
$ret = $html->find('a[class=thumbnail]');
echo $ret;
You were almost there:
<?php
$dom = new DOMDocument();
#$dom->loadHTMLFile('http://www.reddit.com/r/funny');
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a[contains(concat(' ',normalize-space(#class),' '),' thumbnail ')]");
var_dump($hrefs);
Gives:
class DOMNodeList#28 (1) {
public $length =>
int(25)
}
25 matches, I'd call it success.
This code would probably work:
$html = file_get_contents('http://www.reddit.com/r/funny');
$dom = new DOMDocument();
#$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$hyperlinks = $xpath->query('//a[#class="thumbnail"]');
foreach($hyperlinks as $hyperlink) {
echo $hyperlink->getAttribute('href'), '<br>;'
}
if you're using simple_html_dom, why are you doing all these superfluous things? It already wraps the resource in everything you need -- http://simplehtmldom.sourceforge.net/manual.htm
include('simple_html_dom.php');
// set up:
$html = new simple_html_dom();
// load from URL:
$html->load_file('http://www.reddit.com/r/funny');
// find those <a> elements:
$links = $html->find('a[class=thumbnail]');
// done.
echo $links;
Tested it and made some changes - this works perfect too.
<?php
// load the url and set up an array for the links
$dom = new DOMDocument();
#$dom->loadHTMLFile('http://www.reddit.com/r/funny');
$links = array();
// loop thru all the A elements found
foreach($dom->getElementsByTagName('a') as $link) {
$url = $link->getAttribute('href');
$class = $link->getAttribute('class');
// Check if the URL is not empty and if the class contains thumbnail
if(!empty($url) && strpos($class,'thumbnail') !== false) {
array_push($links, $url);
}
}
// Print results
print_r($links);
?>
How can I extract the number 12345 from the following string in PHP ?
<span id="jordan934" itemprop="distance"><span class='WebDistance'>#$#20B9; </span>12345</span></h3>
I was using the following until that '#$#20B9' string was not in it .
$results = $dom->query('#jordan934"]');
$distance = false;
if (count($results)) {
$distance = (int)trim($results->current()->textContent);
}
return $distance;
}
Try using regular expression
$str = 'jordan934';
preg_match_all('!\d+!', $str, $matches);
print_r($matches);
You could use a dom object
<?php
$html = '<span id="jordan934" itemprop="distance"><span class=\'WebDistance\'>#$#20B9; </span>12345</span></h3>';
$dom = new DomDocument();
$dom->loadHTML($filePath);
$finder = new DomXPath($dom);
$classname="my-class";
$nodes = $finder->query("//*[contains(#class, '$classname')]");
var_dump($nodes);
Other not widely known function is:
$str = 'jordan934';
$int = filter_var($str, FILTER_SANITIZE_NUMBER_INT);
http://php.net/manual/pl/function.filter-var.php
I'm following a simplified version of the scraping tutorial by NetTuts here, which basically finds all divs with class=preview
http://net.tutsplus.com/tutorials/php/html-parsing-and-screen-scraping-with-the-simple-html-dom-library/comment-page-1/#comments
This is my code. The problem is that when I count $items I get only 1, so it's getting only the first div with class=preview, not all of them.
$articles = array();
$html = new simple_html_dom();
$html->load_file('http://net.tutsplus.com/page/76/');
$items = $html->find('div[class=preview]');
echo "count: " . count($items);
Try using DOMDocument and DOMXPath:
$file = file_get_contents('http://net.tutsplus.com/page/76/');
$dom = new DOMDocument();
#$dom->loadHTML($file);
$domx = new DOMXPath($dom);
$nodelist = $domx->evaluate("//div[#class='preview']");
foreach ($nodelist as $node) { print $node->nodeValue; }