php 如何从字符串中获取前5个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3787540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get first 5 characters from string
提问by faressoft
How to get first 5 characters from string using php
如何使用php从字符串中获取前5个字符
$myStr = "HelloWordl";
result should be like this
结果应该是这样的
$result = "Hello";
回答by Gumbo
For single-byte strings(e.g. US-ASCII, ISO 8859 family, etc.) use substr
and for multi-byte strings(e.g. UTF-8, UTF-16, etc.) use mb_substr
:
对于单字节字符串(例如 US-ASCII、ISO 8859 系列等)使用substr
,对于多字节字符串(例如 UTF-8、UTF-16 等)使用mb_substr
:
// singlebyte strings
$result = substr($myStr, 0, 5);
// multibyte strings
$result = mb_substr($myStr, 0, 5);
回答by Paul Hodel
An alternative way to get only one character.
仅获取一个字符的另一种方法。
$str = 'abcdefghij';
echo $str{5};