in_array() always false...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jeffrydell
Forum Commoner
Posts: 77
Joined: Thu Jan 17, 2008 4:39 pm
Location: Menasha, WI

in_array() always false...

Post 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
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: in_array() always false...

Post 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.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: in_array() always false...

Post 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>';
    }
}
?>
ody3307
Forum Newbie
Posts: 21
Joined: Wed Jul 30, 2008 7:29 am

Re: in_array() always false...

Post 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.
Post Reply