拆分变量的第一个字母 [英] Split the first letter from variable

查看:113
本文介绍了拆分变量的第一个字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从变量$ name拆分第一个字母。
我该怎么办?

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

$name = $userData['name'];

我如何获得第一封信?

请检查是否正确:

if($userData['gender'] == 'male'){
    if($firstletter=='i'){
    $picture = ImageCreateFromPNG ($global['maleBackgroundImages'][1]);
    }else{
          $picture = ImageCreateFromPNG ($global['maleBackgroundImages'][2]);}
}else{
    if($firstletter=='i'){
    $picture = ImageCreateFromPNG ($global['maleBackgroundImages'][1]);
    }else{
          $picture = ImageCreateFromPNG ($global['maleBackgroundImages'][2]);}
}


推荐答案

$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) */)

这篇关于拆分变量的第一个字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆