PHP中的string类型使用说明


Posted in PHP onJuly 27, 2010

注意:PHP没有对string的长度做限制。唯一限制的就是PHP在计算机中的可用内存(php.ini文件中的memory_limit变量的值)
限定字符串范围的方法有4中:
1、单引号;
2、双引号;
3、原型文档语法;
4、nowdoc syntax(PHP5.3.0开始)

1、如果字符串使用单引号“‘”包裹,字符串中如果出现单引号“,”和反斜杠“\”符号,需要进行转义。

// Outputs: Arnold once said: "I'll be back" 
echo 'Arnold once said: "I\'ll be back"'; 
// Outputs: You deleted C:\*.*? 
echo 'You deleted C:\\*.*?'; 
// Outputs: You deleted C:\*.*? 
echo 'You deleted C:\*.*?';

(有待验证 单引号包裹的字符串反斜杠是否需要转义)

2、如果字符串被双引号包裹 一下字符都会被转义:
Escaped characters Sequence Meaning
\n linefeed (LF or 0x0A (10) in ASCII)
\r carriage return (CR or 0x0D (13) in ASCII)
\t horizontal tab (HT or 0x09 (9) in ASCII)
\v vertical tab (VT or 0x0B (11) in ASCII) (since PHP 5.2.5)
\f form feed (FF or 0x0C (12) in ASCII) (since PHP 5.2.5)
\\ backslash
\$ dollar sign
\" double-quote
\[0-7]{1,3} the sequence of characters matching the regular expression is a character in octal notation
\x[0-9A-Fa-f]{1,2} the sequence of characters matching the regular expression is a character in hexadecimal notation

如果字符串 使用双引号“"”或者原形文档语法的形式包裹的话,在字符串中的变量会被解析。
1、简单语法:
因为解析器会贪婪匹配$后面的字符,所以,为了不出什么以外,应该使用"{"和"}"来表名变量的边界。

<?php 
$beer = 'Heineken'; 
echo "$beer's taste is great"; // works; "'" is an invalid character for variable names 
echo "He drank some $beers"; // won't work; 's' is a valid character for variable names but the variable is "$beer" 
echo "He drank some ${beer}s"; // works 
echo "He drank some {$beer}s"; // works 
?>

同样,数组的下标和对象的属性也会不解析。
<?php 
// These examples are specific to using arrays inside of strings. 
// When outside of a string, always quote array string keys and do not use 
// {braces}. 
// Show all errors 
error_reporting(E_ALL); 
$fruits = array('strawberry' => 'red', 'banana' => 'yellow'); 
// Works, but note that this works differently outside a string 
echo "A banana is $fruits[banana]."; 
// Works 
echo "A banana is {$fruits['banana']}."; 
// Works, but PHP looks for a constant named banana first, as described below. 
echo "A banana is {$fruits[banana]}."; 
// Won't work, use braces. This results in a parse error. 
echo "A banana is $fruits['banana']."; 
// Works 
echo "A banana is " . $fruits['banana'] . "."; 
// Works 
echo "This square is $square->width meters broad."; 
// Won't work. For a solution, see the complex syntax. 
echo "This square is $square->width00 centimeters broad."; 
?>

2、复合语法:
<?php 
// Show all errors 
error_reporting(E_ALL); 
$great = 'fantastic'; 
// Won't work, outputs: This is { fantastic} 
echo "This is { $great}"; 
// Works, outputs: This is fantastic 
echo "This is {$great}"; 
echo "This is ${great}"; 
// Works 
echo "This square is {$square->width}00 centimeters broad."; 
// Works 
echo "This works: {$arr[4][3]}"; 
// This is wrong for the same reason as $foo[bar] is wrong outside a string. 
// In other words, it will still work, but only because PHP first looks for a 
// constant named foo; an error of level E_NOTICE (undefined constant) will be 
// thrown. 
echo "This is wrong: {$arr[foo][3]}"; 
// Works. When using multi-dimensional arrays, always use braces around arrays 
// when inside of strings 
echo "This works: {$arr['foo'][3]}"; 
// Works. 
echo "This works: " . $arr['foo'][3]; 
echo "This works too: {$obj->values[3]->name}"; 
echo "This is the value of the var named $name: {${$name}}"; 
echo "This is the value of the var named by the return value of getName(): {${getName()}}"; 
echo "This is the value of the var named by the return value of \$object->getName(): {${$object->getName()}}";

访问,修改字符串中的指定字符:
字符串可以使用"[]"和"{}"进行访问。(注意:php5.3.0以后不建议使用“{}”访问)
注意:使用其他类型(非integer)类型访问字符串指定的字符,都会返回NULL
警告:
Writing to an out of range offset pads the string with spaces. Non-integer types are converted to integer. Illegal offset type emits E_NOTICE. Negative offset emits E_NOTICE in write but reads empty string. Only the first character of an assigned string is used. Assigning empty string assigns NUL byte。
PHP 相关文章推荐
php mysql数据库操作类
Jun 04 PHP
php下封装较好的数字分页方法
Nov 23 PHP
window+nginx+php环境配置 附配置搭配说明
Dec 29 PHP
用php解析html的实现代码
Aug 08 PHP
PHP文章采集URL补全函数(FormatUrl)
Aug 02 PHP
php中namespace use用法实例分析
Jan 22 PHP
实例讲解PHP设计模式编程中的简单工厂模式
Feb 29 PHP
PHPExcel导出2003和2007的excel文档功能示例
Jan 04 PHP
详解php用static方法的原因
Sep 12 PHP
PHP大文件分片上传的实现方法
Oct 28 PHP
Linux下安装Memcached服务器和客户端与PHP使用示例
Apr 15 PHP
TP5多入口设置实例讲解
Dec 15 PHP
PHP中的array数组类型分析说明
Jul 27 #PHP
ionCube 一款类似zend的PHP加密/解密工具
Jul 25 #PHP
PHP array 的加法操作代码
Jul 24 #PHP
PHP IN_ARRAY 函数使用注意事项
Jul 24 #PHP
PHP STRING 陷阱原理说明
Jul 24 #PHP
PHP下操作Linux消息队列完成进程间通信的方法
Jul 24 #PHP
php抓取页面与代码解析 推荐
Jul 23 #PHP
You might like
PHP getallheaders无法获取自定义头(headers)的问题
2016/03/23 PHP
php微信开发之自定义菜单完整流程
2016/10/08 PHP
php使用Swoole实现毫秒级定时任务的方法
2020/09/04 PHP
JS+XML 省份和城市之间的联动实现代码
2009/10/14 Javascript
很棒的学习jQuery的12个网站推荐
2011/04/28 Javascript
编写高性能Javascript代码的N条建议
2015/10/12 Javascript
jquery插件jquery.dragscale.js实现拖拽改变元素大小的方法(附demo源码下载)
2016/02/25 Javascript
JavaScript String 对象常用方法总结
2016/04/28 Javascript
JQueryEasyUI之DataGrid数据显示
2016/11/23 Javascript
JS实现线性表的顺序表示方法示例【经典数据结构】
2017/04/11 Javascript
教你5分钟学会用requirejs(必看篇)
2017/07/25 Javascript
vue事件修饰符和按键修饰符用法总结
2017/07/25 Javascript
vue组件之Alert的实现代码
2017/10/17 Javascript
详解vue-property-decorator使用手册
2019/07/29 Javascript
Node.js 深度调试方法解析
2020/07/28 Javascript
[26:50]2018完美盛典DOTA2表演赛
2018/12/17 DOTA
[58:54]EG vs RNG 2019国际邀请赛小组赛 BO2 第一场 8.16
2019/08/18 DOTA
python插入数据到列表的方法
2015/04/30 Python
python在windows下创建隐藏窗口子进程的方法
2015/06/04 Python
python 递归深度优先搜索与广度优先搜索算法模拟实现
2018/10/22 Python
python模拟键盘输入 切换键盘布局过程解析
2019/08/15 Python
HTML5中的Web Notification桌面通知功能的实现方法
2019/07/29 HTML / CSS
Marc Jacobs彩妆官网:Marc Jacobs Beauty
2017/07/03 全球购物
英国领先的杂志订阅网站:Magazine.co.uk
2018/01/25 全球购物
马德里竞技官方网上商店:Atletico Madrid Shop
2019/03/31 全球购物
德国的大型美妆个护电商:Flaconi
2020/06/26 全球购物
数据管理员的自我评价分享
2013/11/15 职场文书
党务公开方案
2014/05/06 职场文书
关于运动会广播稿50字
2014/10/18 职场文书
综治维稳工作汇报
2014/10/27 职场文书
营销与策划实训报告
2014/11/05 职场文书
党员廉洁自律个人总结
2015/02/13 职场文书
2015年教学副校长工作总结
2015/07/22 职场文书
《扇形统计图》教学反思
2016/02/17 职场文书
python spilt()分隔字符串的实现示例
2021/05/21 Python
CentOS安装Nginx并部署vue
2022/04/12 Servers