Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

242
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda