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程序员最常犯的11个MySQL错误小结
Nov 20 PHP
PHP求小于1000的所有水仙花数的代码
Jan 10 PHP
php删除文件夹及其文件夹下所有文件的函数代码
Jan 23 PHP
关于php操作mysql执行数据库查询的一些常用操作汇总
Jun 24 PHP
php根据isbn书号查询amazon网站上的图书信息的示例
Feb 13 PHP
smarty模板判断数组为空的方法
Jun 10 PHP
PHP strip_tags保留多个HTML标签的方法
May 22 PHP
示例详解Laravel的注册重构
Aug 14 PHP
php的api数据接口书写实例(推荐)
Sep 22 PHP
Laravel框架模板继承操作示例
Jun 11 PHP
php高清晰度无损图片压缩功能的实现代码
Dec 09 PHP
php和redis实现秒杀活动的流程
Jul 17 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
Smarty变量调节器失效的解决办法
2014/08/20 PHP
php函数serialize()与unserialize()用法实例
2014/11/06 PHP
PHP基于GD库的图像处理方法小结
2016/09/27 PHP
关于ThinkPhp 框架表单验证及ajax验证问题
2017/07/19 PHP
PHP异常处理定义与使用方法分析
2017/07/25 PHP
用JavaScript获取DOM元素位置和尺寸大小的方法
2013/04/12 Javascript
JS 屏蔽按键效果与改变按键效果的示例代码
2013/12/24 Javascript
两个select多选模式的选项相互移动(示例代码)
2014/01/11 Javascript
Javascript浮点数乘积运算出现多位小数的解决方法
2014/02/17 Javascript
jquery禁用右键单击功能屏蔽F5刷新
2014/03/17 Javascript
当滚动条滚动到页面底部自动加载增加内容的js代码
2014/05/13 Javascript
JavaScript使表单中的内容显示在屏幕上的方法
2015/06/29 Javascript
JS自动倒计时30秒后按钮才可用(两种场景)
2015/08/31 Javascript
Bootstrap3 input输入框插入glyphicon图标的方法
2016/05/16 Javascript
layui给下拉框、按钮状态、时间赋初始值的方法
2019/09/10 Javascript
python为tornado添加recaptcha验证码功能
2014/02/26 Python
一个检测OpenSSL心脏出血漏洞的Python脚本分享
2014/04/10 Python
详解在Python的Django框架中创建模板库的方法
2015/07/20 Python
梯度下降法介绍及利用Python实现的方法示例
2017/07/12 Python
Flask模拟实现CSRF攻击的方法
2018/07/24 Python
python遍历小写英文字母的方法
2019/01/02 Python
python提取log文件内容并画出图表
2019/07/08 Python
Flask中endpoint的理解(小结)
2019/12/11 Python
德国奢侈品网上商城:Mytheresa
2016/08/24 全球购物
倩碧英国官网:Clinique英国
2018/08/10 全球购物
Lungolivigno Fashion官网:高级时装在线购物
2020/10/17 全球购物
综合内勤岗位职责
2014/04/14 职场文书
安全生产月演讲稿
2014/05/09 职场文书
节水标语大全
2014/06/11 职场文书
领导班子群众路线与四风问题对照检查材料思想汇报
2014/10/11 职场文书
党员个人整改措施
2014/10/24 职场文书
2014年卫生工作总结
2014/11/27 职场文书
公司放假通知范文
2015/04/14 职场文书
幼儿园教师辞职信
2019/06/21 职场文书
详解CSS伪元素的妙用单标签之美
2021/05/25 HTML / CSS
Python卷积神经网络图片分类框架详解分析
2021/11/07 Python