Getting the first character of a string with $str[0]

前端 未结 9 1137
余生分开走
余生分开走 2020-11-28 21:04

I want to get the first letter of a string and I\'ve noticed that $str[0] works great. I am just not sure whether this is \'good practice\', as that notation is

9条回答
  •  爱一瞬间的悲伤
    2020-11-28 21:47

    In case of multibyte (unicode) strings using str[0] can cause a trouble. mb_substr() is a better solution. For example:

    $first_char = mb_substr($title, 0, 1);
    

    Some details here: Get first character of UTF-8 string

提交回复
热议问题