Search code examples
phpweb-scrapingsimple-html-dom

Why are my "if statement" parameters not filtering my results properly?


I can clearly see in my results that one of the output results is "general". When I try to filter this out in my "if statement", it fails to catch the "general" everytime. My "str_replace" is an attempt to rid the results of any empty white space that might be causing the issue.

Code Snippet:

$tick = 0;
foreach($html->find('select.js-career-select') as $info) {

foreach($info->find('option') as $info2) {
    ++$tick;
    $general = 'general';

    if($tick > 38) {

        $list = $info2;
        $list = strtolower(str_replace(' ', '', $list));

        if($list != $general) {
            echo $list."<br>";
        }
        else {
            echo "NOPE!";
        }
      }
   }
}

Solution

  • I suspect $list has newlines before or after it. Try:

    $list = strtolower(trim(strip_tags($list)));
    

    to remove all types of whitespace surrounding the text, and any HTML tags in the text.

    You can also get just the text from the tag with:

    $list = $info2->plaintext;