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制作静态网站的模板框架(一)
Oct 09 PHP
php单件模式结合命令链模式使用说明
Sep 07 PHP
PHP 写文本日志实现代码
May 18 PHP
php json与xml序列化/反序列化
Oct 28 PHP
PHP之uniqid()函数用法
Nov 03 PHP
ThinkPHP控制器里javascript代码不能执行的解决方法
Nov 22 PHP
ThinkPHP中处理表单中的注意事项
Nov 22 PHP
php实现两个数组相加的方法
Feb 17 PHP
PHP网络操作函数汇总
May 18 PHP
PHP获取网站中各文章的第一张图片的代码示例
May 20 PHP
PHP构造函数与析构函数用法示例
Sep 28 PHP
PHP实现基于图的深度优先遍历输出1,2,3...n的全排列功能
Nov 10 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中的四舍五入函数代码(floor函数、ceil函数、round与intval)
2014/07/14 PHP
Thinkphp3.2.3分页使用实例解析
2016/07/28 PHP
php mysql 封装类实例代码
2016/09/18 PHP
PHP session会话操作技巧小结
2016/09/27 PHP
使用laravel根据用户类型来显示或隐藏字段
2019/10/17 PHP
php ActiveMQ的安装与使用方法图文教程
2020/02/23 PHP
PHP编程一定要改掉的5个不良习惯
2020/09/18 PHP
ExtJS扩展 垂直tabLayout实现代码
2009/06/21 Javascript
js+xml生成级联下拉框代码
2012/07/24 Javascript
js关闭子窗体刷新父窗体实现方法
2012/12/04 Javascript
用jquery.sortElements实现table排序
2014/05/04 Javascript
Javascript函数式编程简单介绍
2015/10/11 Javascript
谈谈Jquery中的children find 的区别有哪些
2015/10/19 Javascript
利用pm2部署多个node.js项目的配置教程
2017/10/22 Javascript
Angular4集成ng2-file-upload的上传组件
2018/03/14 Javascript
Node.js中,在cmd界面,进入退出Node.js运行环境的方法
2018/05/12 Javascript
利用Node.js如何实现文件循环覆写
2019/04/05 Javascript
Vue实现导航栏点击当前标签变色功能
2020/08/19 Javascript
使用vuex存储用户信息到localStorage的实例
2019/11/11 Javascript
python虚拟环境 virtualenv的简单使用
2020/01/21 Javascript
[03:40]DOTA2抗疫特别篇《英雄年代》
2020/02/28 DOTA
python thread 并发且顺序运行示例
2009/04/09 Python
Django命名URL和反向解析URL实现解析
2019/08/09 Python
python-web根据元素属性进行定位的方法
2019/12/13 Python
将matplotlib绘图嵌入pyqt的方法示例
2020/01/08 Python
Python求区间正整数内所有素数之和的方法实例
2020/10/13 Python
使用CSS3的::selection改变选中文本颜色的方法
2015/09/29 HTML / CSS
澳大利亚足球鞋和服装购物网站:Ultra Football
2018/10/11 全球购物
研发工程师的岗位职责
2013/11/18 职场文书
学生自我鉴定模板
2013/12/30 职场文书
电气工程及其自动化专业求职信
2014/06/23 职场文书
二人合伙经营协议书
2014/09/13 职场文书
领导干部群众路线剖析材料
2014/10/09 职场文书
司法局群众路线教育实践活动整改措施思想汇报
2014/10/13 职场文书
幼儿园大班教师个人总结
2015/02/05 职场文书
Apache Kafka 分区重分配的实现原理解析
2022/07/15 Servers