Page 1 of 1

in_array() always false...

Posted: Fri Aug 01, 2008 2:53 pm
by jeffrydell
In trying to write a script that will 'look' at various URL's and detect certain text within the resulting pages, I'm using file() to put the page contents in an array, then in_array() to check for the specific text.

I'm a bit stymied because in_array() always evaluates FALSE. Any idea why?

Code: Select all

 
<?php
$Page = file("http://www.AgilityEvents.net/sandbox/target.html");
$str = "Hi there!";
echo "Yes ... ";
if (in_array($str, $Page))
{
    echo "TRUE";
}
else
{
    echo "FALSE";
}
?>
 
If you go to the page, you'll see the value for $str right there. Your thoughts?

Thanks in advance for whatever help you can offer!

Jeff

Re: in_array() always false...

Posted: Fri Aug 01, 2008 3:10 pm
by nowaydown1
The array you get back from file is multidimensional. I would recommend using file_get_contents or CURL, then just doing a string comparison using strpos() or something instead.

Re: in_array() always false...

Posted: Fri Aug 01, 2008 3:26 pm
by flying_circus
according to the PHP docs, in_array() *should* work with multidimensional arrays.

For the record, I couldn't find a solution either, other than:

Code: Select all

<?php
$pages = file("http://www.AgilityEvents.net/sandbox/target.html");
$str = '<html>';
 
echo "Yes ... ";
foreach($pages as $page) {
    if(strpos($page,$str)) {
        print $page . '<br>';
        print 'TRUE' . '<br>';
    } else {
        print $page . '<br>';
        print 'FALSE' . '<br>';
    }
}
?>

Re: in_array() always false...

Posted: Fri Aug 01, 2008 4:50 pm
by ody3307
in_array() will only match on an exact match, not a portion of what is stored in the array.

This is what is in your example array:

Code: Select all

Array
(
    [0] => <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 
    [1] => <html><head><meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"><title>target.html</title></head><body>Hi there!<br><br>How's it goin'?</body></html>
)
As you can see, "Hi there!" is not one of the array elements. It's part of one.