I have the following three cases which I would expect to display the same output, but it does something strange for $str1:
<?php
$str1 = "— lorem lorem Alice lorem lorem lorem loremlorem";
// | < Why would a dash make a difference in the the found index?
$str2 = "a lorem lorem Alice lorem lorem lorem loremlorem";
$str3 = " lorem lorem Alice lorem lorem lorem loremlorem";
// The found index is always the same
$foundIndex = mb_stripos($str1, "Alice");
var_dump(substr($str1, $foundIndex - 6, 24), $foundIndex);
$foundIndex = mb_stripos($str2, "Alice");
var_dump(substr($str2, $foundIndex - 6, 24), $foundIndex);
$foundIndex = mb_stripos($str3, "Alice");
var_dump(substr($str3, $foundIndex - 6, 24), $foundIndex);
Output:
string(24) "m lorem Alice lorem lore" << Why is this swapped to the right one char?
int(14)
string(24) "lorem Alice lorem lorem "
int(14)
string(24) "lorem Alice lorem lorem "
int(14)
You can test it here.
I use the operations mb_stripos and substr for searching in strings and this is the PoC.
Why is this and how can I fix the behaviour for strings containing special characters?
From a quick check, I think — occupies three bytes, and substr works by bytes, not by characters. strlen("—") is 3 too...
How can I slice the string by characters, instead of by bytes? Slicing it by bytes won't really work for me. And all the special characters should be handled correctly. If I am not wrong emojis, have different sizes too!