Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

312
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!