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中文URL编解码(urlencode()rawurlencode()
Jul 03 PHP
php使用pack处理二进制文件的方法
Jul 03 PHP
PHP中的use关键字概述
Jul 23 PHP
从零开始学YII2框架(六)高级应用程序模板
Aug 20 PHP
PHP实现图片旋转效果实例代码
Oct 01 PHP
php精确的统计在线人数的方法
Oct 21 PHP
php使用ffmpeg向视频中添加文字字幕的实现方法
May 23 PHP
PHP基于反射机制实现插件的可插拔设计详解
Nov 10 PHP
PHP互换两个变量值的方法(不用第三变量)
Nov 14 PHP
Laravel中七个非常有用但很少人知道的Carbon方法
Sep 21 PHP
docker-compose部署php项目实例详解
Jul 30 PHP
微信小程序结合ThinkPHP5授权登陆后获取手机号
Nov 23 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
使用Sphinx对索引进行搜索
2013/06/25 PHP
PHP答题类应用接口实例
2015/02/09 PHP
PHP动态生成指定大小随机图片的方法
2016/03/25 PHP
PHP中PCRE正则解析代码详解
2019/04/26 PHP
laravel-admin 中列表筛选方法
2019/10/03 PHP
JavaScript 工具库 Cloudgamer JavaScript Library v0.1 发布
2009/10/29 Javascript
JavaScript判断数组是否存在key的简单实例
2016/08/03 Javascript
JS实现二叉查找树的建立以及一些遍历方法实现
2017/04/17 Javascript
Vue 中如何正确引入第三方模块的方法步骤
2019/05/05 Javascript
javascript中如何判断类型汇总
2019/05/14 Javascript
layui 对弹窗 form表单赋值的实现方法
2019/09/04 Javascript
详解Vue后台管理系统开发日常总结(组件PageHeader)
2019/11/01 Javascript
es6中let和const的使用方法详解
2020/02/24 Javascript
解决VUE-Router 同一页面第二次进入不刷新的问题
2020/07/22 Javascript
[47:20]DAC2018 4.4 淘汰赛 Optic vs Mineski 第一场
2018/04/05 DOTA
[01:32]TI奖金增速竟因它再创新高!DOTA2勇士令状不朽珍藏Ⅰ饰品欣赏
2018/05/18 DOTA
python创建线程示例
2014/05/06 Python
python字符串连接方式汇总
2014/08/21 Python
Python中处理unchecked未捕获异常实例
2015/01/17 Python
Python脚本实现网卡流量监控
2015/02/14 Python
Python简单实现网页内容抓取功能示例
2018/06/07 Python
Pandas之Fillna填充缺失数据的方法
2019/06/25 Python
Django Channels 实现点对点实时聊天和消息推送功能
2019/07/17 Python
python爬虫 爬取超清壁纸代码实例
2019/08/16 Python
Python正则表达式急速入门(小结)
2019/12/16 Python
详解字符串在Python内部是如何省内存的
2020/02/03 Python
django 解决自定义序列化返回处理数据为null的问题
2020/05/20 Python
python中if及if-else如何使用
2020/06/02 Python
python 如何用urllib与服务端交互(发送和接收数据)
2021/03/04 Python
Java面试题及答案
2012/09/08 面试题
酒店节能减排方案
2014/05/26 职场文书
湖南省召开党的群众路线教育实践活动总结大会报告
2014/10/21 职场文书
2015年车间主任工作总结
2015/05/21 职场文书
迎新晚会主持词开场白
2015/05/28 职场文书
神州牡丹园的导游词
2019/11/20 职场文书
浅谈Python数学建模之固定费用问题
2021/06/23 Python