Split the first letter from variable

前端 未结 4 1099
挽巷
挽巷 2021-01-23 10:17

I need to split the first letter from variable $name. How I can to do it?

$name = $userData[\'name\'];

How I can to get the first letter?

4条回答
  •  孤城傲影
    2021-01-23 10:31

    $userData['name'][0]
    

    Offsets in strings can be accessed like arrays. Be aware that this assumes your strings are in a single byte encoding. If you have multi-byte encoded strings, you need:

    mb_substr($userData['name'], 0, 1, 'UTF-8' /* (the correct encoding) */)
    

提交回复
热议问题