Home > php教程 > php手册 > php像数组一样存取和修改字符串字符

php像数组一样存取和修改字符串字符

WBOY
Release: 2016-06-06 20:23:54
Original
1587 people have browsed it

PHP中字符串中的字符可以通过一个以0为开始的,用类似数组结构中的方括号包含对应的数字来查找和修改,

如获取第二个$str[1]或$str{1} ,不建议使用{},最好使用[]
测试如下

复制代码 代码如下:


//获取字符最后一个字符
$str = 'phpddt.com';
echo $str[strlen($str)-1]; //m
//修改第一个字符
$str = 'phpddt.com';
$str[0] = 'a';  //ahpddt.com
//方括号中的数字超出范围将会产生空白。 
$str = 'phpddt.com';
$str[100] = 'y';  //phpddt.com y
//如果是非整数类型被转换成整数
$str = 'phpddt.com';
$str['a'] = 'y'; //phpddt.com y
$str = 'phpddt.com';
$str[-1] = 'y'; //负数会出错:Warning: Illegal string offset: -1

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template