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 的 __FILE__ 常量
Jan 15 PHP
用PHP实现维护文件代码
Jun 14 PHP
php抓取页面与代码解析 推荐
Jul 23 PHP
PHP中去除换行解决办法小结(PHP_EOL)
Nov 27 PHP
探讨PHP JSON中文乱码的解决方法详解
Jun 06 PHP
php生成图片缩略图功能示例
Feb 22 PHP
PHP基于关联数组20行代码搞定约瑟夫问题示例
Nov 07 PHP
PHP微信发送推送消息乱码的解决方法
Feb 28 PHP
PHP中number_format()函数的用法讲解
Apr 08 PHP
Laravel 简单实现Ajax滚动加载示例
Oct 22 PHP
php将字符串转换为数组实例讲解
May 05 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的mkdir()函数创建文件夹比较安全的权限设置方法
2014/07/28 PHP
用javascript操作xml
2006/11/04 Javascript
javascript编程起步(第七课)
2007/02/27 Javascript
ASP Json Parser修正版
2009/12/06 Javascript
基于jsTree的无限级树JSON数据的转换代码
2010/07/27 Javascript
快速学习AngularJs HTTP响应拦截器
2015/12/31 Javascript
JQuery解析XML的方法小结
2016/04/02 Javascript
jQuery前端开发35个小技巧
2016/05/24 Javascript
强大Vue.js组件浅析
2016/09/12 Javascript
实例讲解JavaScript中call、apply、bind方法的异同
2016/09/13 Javascript
Bootstrap面板学习使用
2017/02/09 Javascript
webpack2.0搭建前端项目的教程详解
2017/04/05 Javascript
js实现Tab选项卡切换效果
2020/07/17 Javascript
JavaScript中Object值合并方法详解
2017/12/22 Javascript
Vue组件之极简的地址选择器的实现
2018/05/31 Javascript
vue-lazyload使用总结(推荐)
2018/11/01 Javascript
Node.js API详解之 dgram模块用法实例分析
2020/06/05 Javascript
python实现在每个独立进程中运行一个函数的方法
2015/04/23 Python
Python中使用items()方法返回字典元素对的教程
2015/05/21 Python
Python基于回溯法解决01背包问题实例
2017/12/06 Python
对Python 3.2 迭代器的next函数实例讲解
2018/10/18 Python
用xpath获取指定标签下的所有text的实例
2019/01/02 Python
python 画出使用分类器得到的决策边界
2019/08/21 Python
python中shell执行知识点
2020/05/06 Python
从python读取sql的实例方法
2020/07/21 Python
关于python3.9安装wordcloud出错的问题及解决办法
2020/11/02 Python
HTML5拖放API实现自动生成相框功能
2020/04/07 HTML / CSS
英国奢侈品网站:MatchesFashion
2016/12/16 全球购物
Blue Nile台湾:钻石珠宝商,订婚首饰、结婚戒指和精品首饰
2017/11/24 全球购物
采购员岗位职责
2013/11/15 职场文书
法学自荐信
2014/06/20 职场文书
城市规划应届生推荐信
2014/09/08 职场文书
实习工作表现评语
2014/12/31 职场文书
2015年教师党员承诺书
2015/04/27 职场文书
2016五四青年节活动总结范文
2016/04/06 职场文书
攻略丨滑雪究竟该选哪款对讲机?
2022/02/18 无线电