substr works strangely with dashes in the string

Viewed 60

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!

2 Answers

The em dash you're using is a multibyte character. To perform a multi-byte safe substr() operation, you need to use mb_substr().

<?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(mb_substr($str1, $foundIndex - 6, 24), $foundIndex);

$foundIndex = mb_stripos($str2, "Alice");
var_dump(mb_substr($str2, $foundIndex - 6, 24), $foundIndex);

$foundIndex = mb_stripos($str3, "Alice");
var_dump(mb_substr($str3, $foundIndex - 6, 24), $foundIndex);

This is why:

echo strlen('-') . "\n";  // equals 1 using a normal dash
echo strlen('—') . "\n";   // equals 3 using your "em dash"

So if u do echo substr($str1, 0, 3) . "\n"; you get your "em dash".

Related