Reputation: 1
I am trying to use an IF statement to check if a variable contains a piece of text, if so, echo yes, if not, echo no. Simple I know, but it does not seem to be working for me. I am very new to PHP, so any replies should be understandable for a monkey! :P
Here is my code. Bear in mind I am working with PHP simple html dom.
<?php
include 'simple_html_dom.php';
$html = file_get_html('http://www.amazon.co.uk/New-Apple-iPod-touch-Generation/dp/B0040GIZTI/ref=br_lf_m_1000333483_1_1_img?ie=UTF8&s=electronics&pf_rd_p=229345967&pf_rd_s=center-3&pf_rd_t=1401&pf_rd_i=1000333483&pf_rd_m=A3P5ROKL5A1OLE&pf_rd_r=1ZW9HJW2KN2C2MTRJH60');
$stock_data = $html->find('span[class=availGreen]',0);
if ( $stock_data == "In stock." ) {
echo "Yes";
}
echo "No";
?>
Now I have tested if the variable did contain the text, using echo $stock_data; it prints the correct text, however the if statement still comes back and echo's No.
Any help, apologies if this is something so simple.
Upvotes: 0
Views: 700
Reputation: 48304
I don't know much about the library you are using, but if it is returning an object that implements a __toString()
(used by echo), then perhaps this will work:
if ( (string) $stock_data == "In stock." ) {
Otherwise, check for member functions that return the string you are looking for.
I should add this from the manual:
It is worth noting that before PHP 5.2.0 the __toString method was only called when it was directly combined with echo() or print().
So if you are running a more recent PHP, the part of my answer regarding casting to string won't apply. (There still may be some member function you can call to get at the string data minus HTML and special chars.)
Upvotes: 0
Reputation: 542
I tried your code, and $stock_data returns:
<span class="availGreen">In stock.</span>
and not:
In stock.
I suggest that as a new developer, you should get tools like Firebug for Firefox, or Chrome, which can help you debug the underlying errors...although in this case, since html simply outputs the text within the span tags, the only way to know what $stock_data equals to would be to view the source.
Upvotes: 1
Reputation: 1529
I'm not sure what file_get_html() is but you may want to use file_get_contents() instead (probably with DOMDocument and DOMXpath).
$html = file_get_contents('http://www.amazon.co.uk/New-Apple-iPod-touch-Generation/dp/B0040GIZTI/ref=br_lf_m_1000333483_1_1_img?ie=UTF8&s=electronics&pf_rd_p=229345967&pf_rd_s=center-3&pf_rd_t=1401&pf_rd_i=1000333483&pf_rd_m=A3P5ROKL5A1OLE&pf_rd_r=1ZW9HJW2KN2C2MTRJH60');
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXpath($dom);
$in_stock = $xpath->query("/html/body/div[@id='divsinglecolumnminwidth']/form[@id='handleBuy']/table[3]/tbody/tr[3]/td/div/span");
if(!empty($in_stock)) {
echo "yes";
} else {
echo "no";
}
If the same span is occupied when the item is NOT in stock you can test the value like this:
if($in_stock->item(0)->nodeValue == "In stock.") { echo "yes"; } else { echo "no"; }
You may also want to use preg_match if the string "In stock." changes at all.
Upvotes: 0
Reputation: 2404
Use the function
var_dump($stock_data);
Maybe there are non-human readable characters in it and you're not checking against them. You can always trim those
Upvotes: 3