John Doe
John Doe

Reputation: 143

strpos() !== true always evaluates as true

Currently I am having a bunch of issues filtering it so that only post which include the string http://www.example.com/?dl_name= go through.

The first if statement works flawlessly but the second one returns every post that I am scraping even the ones that don't have the url included above what am I doing wrong?

$url = '' . $element->href;
$html2 = file_get_html($url);
$title = $html2->find('.single-style1-title h1', 0);
$s = $title->plaintext;
$str2 = explode ("MUSIC:", $s);
print '<br>';
$date = $html2->find('strong > a', 0);
$n = $date->href;
$str = explode ("http://www.example.com/?dl_name=", $n);
if (strpos($title->plaintext,"VIDEO:") === FALSE) { 
    if (strpos($n,'http://www.example.com/?dl_name=') !== true) {   
        $image = $html2->find('.thumb-wrap img', 0);
        //$date = $html2->find('strong > a', 0);
        //$n = $date->href;
        print '<br><br>';
        echo $url;
        print '<br>';
        print htmlspecialchars_decode($image->src);
        print '<br>';
        print $str2[1];
        print '<br>';
        print $str[1];
    }
}

Upvotes: 1

Views: 2214

Answers (2)

Alec
Alec

Reputation: 361

The problem lies here:

strpos($n,'http://www.example.com/?dl_name=') !== true

strpos never returns true, only FALSE if it is not present, or the first position it occurs. You said that you want strings which contain "http://www.example.com/?dl_name=" right?

Try change it to strpos($n,'http://www.example.com/?dl_name=') !== FALSE and see what happens.

Upvotes: 1

kozlice
kozlice

Reputation: 724

The problem is here:

if (strpos($n,'http://www.example.com/?dl_name=') !== true)

strpos never returns true. Quote from the docs:

Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.

Returns FALSE if the needle was not found.

Upvotes: 3

Related Questions