Skip to content
Advertisement

php substr() and currency symbol strange output

This is a little bit rude code and I am having this output for this simple string formatting sample.

foreach (Transaction::where('customer',$id)->cursor() as $ftr) {
               
               $origValue=$ftr->value;
               $currency= mb_substr($origValue, 0, 1, "UTF-8");
               $value = substr($origValue, 1);

               dd($value);    //
}

$originalValue is equal for example to “$50.00”;

$currency is setted correctly to “$”;

$value should be “50.00” but this is the result of the above dd() is:

b”£50.00″

($originalValue is still the same even after the “mb_substr” operation)

what I have made wrong?

Advertisement

Answer

Here is the problem.

You are trying to return the part of string using substr where your string $origValue has a special characters. So look at the code below

echo substr("£50.00", 1); // will print �50.00
echo mb_substr("£50.00", 1 , 5); // will print 50.00

Also read through this https://www.php.net/manual/en/mbstring.configuration.php#ini.mbstring.func-overload

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement