strcmp() returning -1 for different strings

Viewed 53
<?php
$html = file_get_contents("https://www.casa.gov.au/rpa-operator-certificate-holders");
$pokemon_doc = new DOMDocument();
libxml_use_internal_errors(TRUE);
if(!empty($html)){ 

    $pokemon_doc->loadHTML($html);
    libxml_clear_errors(); 
    $pokemon_xpath  = new DOMXPath($pokemon_doc);
    $pokemon_row    = $pokemon_xpath->query('//tr');

    if($pokemon_row->length > 0){
        $s4 = "4 ONE 4 REAL ESTATE PTY LTD ";
        $s5 = ($pokemon_row[2]->firstChild->nodeValue);
        echo strcmp($s4,$s5); 
    }

}
?>

From the above code I am trying to compare string from the table of this site("https://www.casa.gov.au/rpa-operator-certificate-holders") but I didn't get output as 0 when I am using strcmp(), instead I am getting output as -1

1 Answers

The problem is the extra whitespace around the strings you are comparing. If you add

echo ">".$s4."<>".$s5."<";

you get

>4 ONE 4 REAL ESTATE PTY LTD <>
            4 ONE 4 REAL ESTATE PTY LTD          <

You could easily change the compare to...

echo strcmp(trim($s4),trim($s5));
Related