0

I used exec() to pull out the man pages with the following code:

$oares = array();
exec("man ar", $oares);

foreach ($oares as $kk => $vv) {
    // $oares contain lines of output from `man ar`
    // d() is a custom function from https://kint-php.github.io/kint/, similar to var_dump();
    d($vv);
    d("NAME");
    d(trim($vv) == "NAME");
    d($vv == "NAME");
    d("NAME" == "NAME");
    d(mb_strpos(trim($vv), "NAME"));
}

For one of the iterations of this foreach loop, where $vv = "NAME" (which is a line of output from man ar), the comparisons do not match as they should:

┌─────────────────────────────────────────────────────────────┐
│ $vv                                                         │
└─────────────────────────────────────────────────────────────┘
UTF-8 string (12) "NAME"
┌─────────────────────────────────────────────────────────────┐
│ literal                                                     │
└─────────────────────────────────────────────────────────────┘
string (4) "NAME"
┌─────────────────────────────────────────────────────────────┐
│ trim($vv)                                                   │
└─────────────────────────────────────────────────────────────┘
UTF-8 string (12) "NAME"
┌─────────────────────────────────────────────────────────────┐
│ trim($vv)=="NAME"                                           │
└─────────────────────────────────────────────────────────────┘
boolean false
┌─────────────────────────────────────────────────────────────┐
│ $vv=="NAME"                                                 │
└─────────────────────────────────────────────────────────────┘
boolean false
┌─────────────────────────────────────────────────────────────┐
│ "NAME"=="NAME                                               │
└─────────────────────────────────────────────────────────────┘
boolean true

mb_strpos(trim($vv), "NAME") - also return false

I think it should be because $vv contains a UTF-8 string, while the "NAME" literal is not a UTF-8 string. What do I have to do so that the comparisons and the use of the mb_strpos function will all return true?

user3840170
  • 26,597
  • 4
  • 30
  • 62
forgodsakehold
  • 870
  • 10
  • 26
  • 3
    In the title you say `compared with === operator in PHP` but you're using `==` instead of `===`. – Marco Mar 13 '23 at 15:18
  • 3
    All characters in `NAME` are on codepoints <= 127, so they encode exactly the same in ASCII and UTF-8. Even in UTF-8, `NAME` only takes four bytes. Your debug output says you got _twelve_, so your content is surely _not_ encoded in UTF-8. – CBroe Mar 13 '23 at 15:21
  • 1
    @CBroe is right. You should provide a hexdump of the string. Here's a question on how to make a hexdump of a string: [How can I get a hex dump of a string in PHP?](https://stackoverflow.com/questions/1057572/how-can-i-get-a-hex-dump-of-a-string-in-php) – Marco Mar 13 '23 at 15:41
  • 1
    Try comparing against `"N\x08NA\x08AM\x08ME\x08E"`. – user3840170 Mar 13 '23 at 16:57

0 Answers0