Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

240
Visualizações
substr works strangely with dashes in the string

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!

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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);
over 4 years ago · Santiago Trujillo Relatório

0

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".

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda