2

I am removing the first character of the string and I am using substr(); function for that.
For example :

<?php
$amount =  '€300';
echo substr($amount ,1);
?>

This code is doing its work fine but it have some bug. When I display the substr() function applied string then it display something other symbol at the beginning of the string. Below image has output.enter image description here

In above image you can see its displaying unwanted symbol. But when I apply substr() function again it works successfull.
I just want to know why this function having that symbol? what does this symbol mean? Why its coming in the output?

Martin
  • 22,212
  • 11
  • 70
  • 132
Omkar
  • 298
  • 5
  • 27
  • Seems to work fine in PHPFiddle too. – Mureinik Oct 03 '15 at 08:59
  • 3
    This probably is an issue with your php setup, your configuration. This looks as if you are trying to apply the 8 bit function to a utf encoded string which certainly will not work.Please read about unicode support in php and about the `mbstring` extension and "function overloading"`. – arkascha Oct 03 '15 at 08:59
  • The problem seem to happen with the `€` symbol but not with `$` – Pedro Lobito Oct 03 '15 at 09:00
  • 1
    This happens when you strip a byte of a multibyte character. You should use multibyte-aware string functions (`mb_*`) for multibyte-encoded strings (e. g. UTF-8). http://stackoverflow.com/questions/9087502/php-substr-function-with-utf-8-leaves-marks-at-the-end?lq=1 – Quasdunk Oct 03 '15 at 09:15

2 Answers2

2

you could using mb_substr() php function http://php.net/manual/en/function.mb-substr.php

<?php
$amount =  '€300';
echo mb_substr($amount, 1, NULL, "UTF-8");
?>
Nabi
  • 764
  • 1
  • 10
  • 22
1

I managed to solve the problem by using utf8_decode, i.e.:

$amount =  utf8_decode('€300');
echo substr($amount ,1);
//300
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268