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 相关文章推荐
WINXP下apache+php4+mysql
Nov 25 PHP
PHP 引用是个坏习惯
Mar 12 PHP
php中批量修改文件后缀名的函数代码
Oct 23 PHP
url decode problem 解决方法
Dec 26 PHP
PHP实现根据浏览器跳转不同语言页面代码
Aug 02 PHP
php去除HTML标签实例
Nov 06 PHP
json的键名为数字时的调用方式(示例代码)
Nov 15 PHP
discuz加密解密函数使用方法和中文注释
Jan 21 PHP
Codeigniter操作数据库表的优化写法总结
Jun 12 PHP
php使用正则验证中文
Apr 06 PHP
PHP中使用OpenSSL生成证书及加密解密
Feb 05 PHP
PDO::beginTransaction讲解
Jan 27 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
js中if语句的几种优化代码写法
2011/03/12 Javascript
JS解析json数据并将json字符串转化为数组的实现方法
2012/12/25 Javascript
Javascript call和apply区别及使用方法
2013/11/14 Javascript
jquery实现勾选复选框触发事件给input赋值
2015/02/01 Javascript
JS运动基础框架实例分析
2015/03/03 Javascript
Shell脚本实现Linux系统和进程资源监控
2015/03/05 Javascript
jQuery表单验证功能实例
2015/08/28 Javascript
学习javascript面向对象 javascript实现继承的方式
2016/01/04 Javascript
Bootstrap 最常用的JS插件系列总结(图片轮播、标签切换等)
2016/07/14 Javascript
原生JS获取元素集合的子元素宽度实例
2016/12/14 Javascript
ES6新特性之模块Module用法详解
2017/04/01 Javascript
微信小程序使用video组件播放视频功能示例【附源码下载】
2017/12/08 Javascript
使用vue-router为每个路由配置各自的title
2018/07/30 Javascript
详解JSON和JSONP劫持以及解决方法
2019/03/08 Javascript
教你完全理解ReentrantLock重入锁
2019/06/03 Javascript
vue.js实现数据库的JSON数据输出渲染到html页面功能示例
2019/08/03 Javascript
从请求到响应过程中django都做了哪些处理
2018/08/01 Python
Dlib+OpenCV深度学习人脸识别的方法示例
2019/05/14 Python
numpy ndarray 按条件筛选数组,关联筛选的例子
2019/11/26 Python
Flask之pipenv虚拟环境的实现
2019/11/26 Python
Python使用Turtle模块绘制国旗的方法示例
2021/02/28 Python
品学兼优的大学生自我评价
2013/09/20 职场文书
大学生专科学习生活的自我评价
2013/12/07 职场文书
竞选班长的演讲稿
2014/04/24 职场文书
英语教师自荐信
2014/05/26 职场文书
教师职位说明书
2014/07/29 职场文书
国际贸易系求职信
2014/08/09 职场文书
民事二审代理词
2015/05/25 职场文书
大学生饮品店创业计划书范文
2019/07/10 职场文书
python使用openpyxl库读写Excel表格的方法(增删改查操作)
2021/05/02 Python
python爬虫之selenium库的安装及使用教程
2021/05/23 Python
eval(cmd)与eval($cmd)的区别与联系
2021/07/07 PHP
MySQL中一条update语句是如何执行的
2022/03/16 MySQL
Python IO文件管理的具体使用
2022/03/20 Python
nginx常用配置conf的示例代码详解
2022/03/21 Servers
PostgreSQL常用字符串分割函数整理汇总
2022/07/07 PostgreSQL