37

Will it make any difference or impact on my result, if I use substr() instead of mb_substr() function?

As my server does not have support for mb_ functions, I have to replace it with substr()

Gras Double
  • 15,901
  • 8
  • 56
  • 54
Poonam Bhatt
  • 10,154
  • 16
  • 53
  • 72

3 Answers3

84

If you have utf-8 encoding use mb_substr

Example :

echo substr("hi mémé", 0 , 5); // will print hi m�
echo mb_substr("hi mémé", 0 , 5); // will print hi mé
Saad Achemlal
  • 3,616
  • 5
  • 16
  • 17
16

It will impact your script if you work with multi-byte text that you substring from. If this is the case, I higly recommend enabling mb_* functions in your php.ini or do this ini_set("mbstring.func_overload", 2);

Alin Breaban
  • 284
  • 1
  • 3
5

string substr ( string $string , int $start [, int $length ] )

Returns the portion of string specified by the start and length parameters.

string mb_substr ( string $str , int $start [, int $length [, string $encoding ]] )

Performs a multi-byte safe substr() operation based on number of characters. Position is counted from the beginning of str. First character's position is 0. Second character position is 1, and so on.

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • 1
    Note that the php.ini setting mbstring.func_overload can affect this difference, so that substr actually uses the mb_substr code – Mark Baker Oct 29 '12 at 11:32
  • @Lawrence Cherone : this is also the case with substr().. Position is counted from the beginning of str. First character's position is 0. Second character position is 1 – Poonam Bhatt Oct 29 '12 at 11:33