PHP 访问单个字符

本文最后更新于:2024年3月18日 凌晨

PHP 访问单个字符

  • 函数strlen()返回字符串中的字符数,即字符长度:
1
2
$string = 'Hello, world';
$length = strlen($string); //$length为12
  • 可以在字符串中使用字符串偏移量语法来定位单个字符:
1
2
3
4
5
6
7
8
9
10
$string = 'Hello';
for($i = 0;$i < strlen($string);$i++){
print("The %dth character is %s\n",$i,$string{$i});
}

The 0th character is H
The 1th character is e
The 2th character is l
The 3th character is l
The 4th character is o

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!