Home  >  Article  >  Backend Development  >  php string (String)

php string (String)

伊谢尔伦
伊谢尔伦Original
2016-11-24 14:03:101971browse

一个字符串 string 就是由一系列的字符组成,其中每个字符等同于一个字节。这意味着 PHP 只能支持 256 的字符集,因此不支持 Unicode 。详见字符串类型详解。

Note: string 最大可以达到 2GB。

语法

一个字符串可以用 4 种方式表达:

单引号

双引号

heredoc 语法结构

nowdoc 语法结构(自 PHP 5.3.0 起)

单引号

定义一个字符串的最简单的方法是用单引号把它包围起来(字符 ')。

要表达一个单引号自身,需在它的前面加个反斜线(\)来转义。要表达一个反斜线自身,则用两个反斜线(\\)。其它任何方式的反斜线都会被当成反斜线本身:也就是说如果想使用其它转义序列例如 \r 或者 \n,并不代表任何特殊含义,就单纯是这两个字符本身。

Note: 不像双引号和 heredoc 语法结构,在单引号字符串中的变量和特殊字符的转义序列将不会被替换。

<?php
echo &#39;this is a simple string&#39;;
 
// 可以录入多行
echo &#39;You can also have embedded newlines in
strings this way as it is
okay to do&#39;;
 
// 输出: Arnold once said: "I&#39;ll be back"
echo &#39;Arnold once said: "I\&#39;ll be back"&#39;;
 
// 输出: You deleted C:\*.*?
echo &#39;You deleted C:\\*.*?&#39;;
 
// 输出: You deleted C:\*.*?
echo &#39;You deleted C:\*.*?&#39;;
 
// 输出: This will not expand: \n a newline
echo &#39;This will not expand: \n a newline&#39;;
 
// 输出: Variables do not $expand $either
echo &#39;Variables do not $expand $either&#39;;
?>

双引号

如果字符串是包围在双引号(")中, PHP 将对一些特殊的字符进行解析:

序列

   

含义

   

\n

   

换行(ASCII 字符集中的 LF 或 0x0A (10))

   

\r

   

回车(ASCII 字符集中的 CR 或 0x0D (13))

   

\t

   

水平制表符(ASCII 字符集中的 HT 或 0x09 (9))

   

\v

   

垂直制表符(ASCII 字符集中的 VT 或 0x0B (11))(自 PHP 5.2.5 起)

   

\e

   

Escape(ASCII 字符集中的 ESC 或 0x1B (27))(自 PHP 5.4.0 起)

   

\f

   

换页(ASCII 字符集中的 FF 或 0x0C (12))(自 PHP 5.2.5 起)

   

\\

   

反斜线

   

\$

   

美元标记

   

\"

   

双引号

   

\[0-7]{1,3}

   

符合该正则表达式序列的是一个以八进制方式来表达的字符

   

\x[0-9A-Fa-f]{1,2}

   

符合该正则表达式序列的是一个以十六进制方式来表达的字符

   

和单引号字符串一样,转义任何其它字符都会导致反斜线被显示出来。PHP 5.1.1 以前,\{$var} 中的反斜线还不会被显示出来。

用双引号定义的字符串最重要的特征是变量会被解析,详见变量解析。

Heredoc 结构

第三种表达字符串的方法是用 heredoc 句法结构:91ab36d77c9c76d31ffa2faecff8c984 结构是用来声明大段的不用解析的文本类似,nowdoc 结构也有相同的特征。

一个 nowdoc 结构也用和 heredocs 结构一样的标记 bef3a2e20cd31fb928f61ce95251476bfoo.
Now, I am printing some {$foo->bar[1]}.
This should not print a capital 'A': \x41

Note:

不象 heredoc 结构,nowdoc 结构可以用在任意的静态数据环境中,最典型的示例是用来初始化类的属性或常量:

Example #7 静态数据的示例

<?php
class foo {
   public $bar = <<<&#39;EOT&#39;
bar
EOT;
}
?>

Note:

Nowdoc 结构是在 PHP 5.3.0 中加入的。

变量解析

当字符串用双引号或 heredoc 结构定义时,其中的变量将会被解析。

这里共有两种语法规则:一种简单规则,一种复杂规则。简单的语法规则是最常用和最方便的,它可以用最少的代码在一个 string 中嵌入一个变量,一个 array 的值,或一个 object 的属性。

复杂规则语法的显著标记是用花括号包围的表达式。

简单语法

当 PHP 解析器遇到一个美元符号($)时,它会和其它很多解析器一样,去组合尽量多的标识以形成一个合法的变量名。可以用花括号来明确变量名的界线。

<?php
$juice = "apple";
 
echo "He drank some $juice juice.".PHP_EOL;
// Invalid. "s" is a valid character for a variable name, but the variable is $juice.
echo "He drank some juice made of $juices.";
?>

以上例程会输出:

He drank some apple juice.
He drank some juice made of .

类似的,一个 array 索引或一个 object 属性也可被解析。数组索引要用方括号(])来表示索引结束的边际,对象属性则是和上述的变量规则相同。

Example #8 简单语法示例

<?php
$juices = array("apple", "orange", "koolaid1" => "purple");
 
echo "He drank some $juices[0] juice.".PHP_EOL;
echo "He drank some $juices[1] juice.".PHP_EOL;
echo "He drank some juice made of $juice[0]s.".PHP_EOL; // Won&#39;t work
echo "He drank some $juices[koolaid1] juice.".PHP_EOL;
 
class people {
   public $john = "John Smith";
   public $jane = "Jane Smith";
   public $robert = "Robert Paulsen";
   public $smith = "Smith";
}
 
$people = new people();
 
echo "$people->john drank some $juices[0] juice.".PHP_EOL;
echo "$people->john then said hello to $people->jane.".PHP_EOL;
echo "$people->john&#39;s wife greeted $people->robert.".PHP_EOL;
echo "$people->robert greeted the two $people->smiths."; // Won&#39;t work
?>

以上例程会输出:

He drank some apple juice.
He drank some orange juice.
He drank some juice made of s.
He drank some purple juice.
John Smith drank some apple juice.
John Smith then said hello to Jane Smith.
John Smith's wife greeted Robert Paulsen.
Robert Paulsen greeted the two .

如果想要表达更复杂的结构,请用复杂语法。

复杂(花括号)语法

复杂语法不是因为其语法复杂而得名,而是因为它可以使用复杂的表达式。

任何具有 string 表达的标量变量,数组单元或对象属性都可使用此语法。只需简单地像在 string 以外的地方那样写出表达式,然后用花括号 { 和 } 把它括起来即可。由于 { 无法被转义,只有 $紧挨着 { 时才会被识别。可以用 {\$ 来表达 {$。下面的示例可以更好的解释:

<?php
// 显示所有错误
error_reporting(E_ALL);
 
$great = &#39;fantastic&#39;;
 
// 无效,输出: This is { fantastic}
echo "This is { $great}";
 
// 有效,输出: This is fantastic
echo "This is {$great}";
echo "This is ${great}";
 
// 有效
echo "This square is {$square->width}00 centimeters broad.";
 
// 有效,只有通过花括号语法才能正确解析带引号的键名
echo "This works: {$arr[&#39;key&#39;]}";
 
// 有效
echo "This works: {$arr[4][3]}";
 
// 这是错误的表达式,因为就象 $foo[bar] 的格式在字符串以外也是错的一样。
// 换句话说,只有在 PHP 能找到常量 foo 的前提下才会正常工作;这里会产生一个
// E_NOTICE (undefined constant) 级别的错误。
echo "This is wrong: {$arr[foo][3]}";
 
// 有效,当在字符串中使用多重数组时,一定要用括号将它括起来
echo "This works: {$arr[&#39;foo&#39;][3]}";
 
// 有效
echo "This works: " . $arr[&#39;foo&#39;][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()}}";
 
// 无效,输出: This is the return value of getName(): {getName()}
echo "This is the return value of getName(): {getName()}";
?>

 

也可以在字符串中用此语法通过变量来调用类的属性。

<?php
class foo {
   var $bar = &#39;I am bar.&#39;;
}
 
$foo = new foo();
$bar = &#39;bar&#39;;
$baz = array(&#39;foo&#39;, &#39;bar&#39;, &#39;baz&#39;, &#39;quux&#39;);
echo "{$foo->$bar}\n";
echo "{$foo->$baz[1]}\n";
?>

以上例程会输出:

I am bar.

I am bar.

Note:

函数、方法、静态类变量和类常量只有在 PHP 5 以后才可在 {$} 中使用。然而,只有在该字符串被定义的命名空间中才可以将其值作为变量名来访问。只单一使用花括号 ({}) 无法处理从函数或方法的返回值或者类常量以及类静态变量的值。

<?php
// 显示所有错误
error_reporting(E_ALL);
 
class beers {
   const softdrink = &#39;rootbeer&#39;;
   public static $ale = &#39;ipa&#39;;
}
 
$rootbeer = &#39;A & W&#39;;
$ipa = &#39;Alexander Keith\&#39;s&#39;;
 
// 有效,输出: I&#39;d like an A & W
echo "I&#39;d like an {${beers::softdrink}}\n";
 
// 也有效,输出: I&#39;d like an Alexander Keith&#39;s
echo "I&#39;d like an {${beers::$ale}}\n";
?>

存取和修改字符串中的字符

string 中的字符可以通过一个从 0 开始的下标,用类似 array 结构中的方括号包含对应的数字来访问和修改,比如 $str[42]。可以把 string 当成字符组成的 array。函数 substr() 和substr_replace() 可用于操作多于一个字符的情况。

Note: string 也可用花括号访问,比如 $str{42}。

Warning

用超出字符串长度的下标写入将会拉长该字符串并以空格填充。非整数类型下标会被转换成整数。非法下标类型会产生一个 E_NOTICE 级别错误。用负数下标写入字符串时会产生一个 E_NOTICE 级别错误,用负数下标读取字符串时返回空字符串。写入时只用到了赋值字符串的第一个字符。用空字符串赋值则赋给的值是 NULL 字符。

Warning

PHP 的字符串在内部是字节组成的数组。因此用花括号访问或修改字符串对多字节字符集很不安全。仅应对单字节编码例如 ISO-8859-1 的字符串进行此类操作。

Example #9 一些字符串示例

<?php
// 取得字符串的第一个字符
$str = &#39;This is a test.&#39;;
$first = $str[0];
 
// 取得字符串的第三个字符
$third = $str[2];
 
// 取得字符串的最后一个字符
$str = &#39;This is still a test.&#39;;
$last = $str[strlen($str)-1];
 
// 修改字符串的最后一个字符
$str = &#39;Look at the sea&#39;;
$str[strlen($str)-1] = &#39;e&#39;;
?>
自 PHP 5.4 起字符串下标必须为整数或可转换为整数的字符串,否则会发出警告。之前例如 "foo" 的下标会无声地转换成 0。
Example #10 PHP 5.3 和 PHP 5.4 的区别
<?php
$str = &#39;abc&#39;;
 
var_dump($str[&#39;1&#39;]);
var_dump(isset($str[&#39;1&#39;]));
 
var_dump($str[&#39;1.0&#39;]);
var_dump(isset($str[&#39;1.0&#39;]));
 
var_dump($str[&#39;x&#39;]);
var_dump(isset($str[&#39;x&#39;]));
 
var_dump($str[&#39;1x&#39;]);
var_dump(isset($str[&#39;1x&#39;]));
?>

以上例程在PHP 5.3中的输出:

string(1) "b"
bool(true)
string(1) "b"
bool(true)
string(1) "a"
bool(true)
string(1) "b"
bool(true)

以上例程在PHP 5.4中的输出:

string(1) "b"
bool(true)

Warning: Illegal string offset '1.0' in /tmp/t.php on line 7
string(1) "b"
bool(false)

Warning: Illegal string offset 'x' in /tmp/t.php on line 9
string(1) "a"
bool(false)
string(1) "b"
bool(false)

Note:

用 [] 或 {} 访问任何其它类型(不包括数组或具有相应接口的对象实现)的变量只会无声地返回 NULL。

Note:

PHP 5.5 增加了直接在字符串原型中用 [] 或 {} 访问字符的支持。

有用的函数和运算符

字符串可以用 '.'(点)运算符连接起来,注意 '+'(加号)运算符没有这个功能。更多信息参考字符串运算符。

对于 string 的操作有很多有用的函数。

可以参考字符串函数了解大部分函数,高级的查找与替换功能可以参考正则表达式函数或 Perl 兼容正则表达式函数。

另外还有 URL 字符串函数,也有加密/解密字符串的函数(mcrypt 和 mhash)。

最后,可以参考字符类型函数。

转换成字符串

一个值可以通过在其前面加上 (string) 或用 strval() 函数来转变成字符串。在一个需要字符串的表达式中,会自动转换为 string。比如在使用函数 echo 或 print 时,或在一个变量和一个 string进行比较时,就会发生这种转换。类型和类型转换可以更好的解释下面的事情,也可参考函数 settype()。

一个布尔值 boolean 的 TRUE 被转换成 string 的 "1"。Boolean 的 FALSE 被转换成 ""(空字符串)。这种转换可以在 boolean 和 string 之间相互进行。

一个整数 integer 或浮点数 float 被转换为数字的字面样式的 string(包括 float 中的指数部分)。使用指数计数法的浮点数(4.1E+6)也可转换。

Note:

在脚本的区域(category LC_NUMERIC)中定义了十进制小数点字符。参见 setlocale()。

数组 array 总是转换成字符串 "Array",因此, echo 和 print 无法显示出该数组的内容。要显示某个单元,可以用 echo $arr['foo'] 这种结构。要显示整个数组内容见下文。

在 PHP 4 中对象 object 总是被转换成字符串 "Object",如果为了调试原因需要打印出对象的值,请继续阅读下文。为了得到对象的类的名称,可以用 get_class() 函数。自 PHP 5 起,适当时可以用 __toString 方法。

资源 resource 总会被转变成 "Resource id #1" 这种结构的字符串,其中的 1 是 PHP 在运行时分配给该 resource 的唯一值。不要依赖此结构,可能会有变更。要得到一个 resource 的类型,可以用函数 get_resource_type()。

NULL 总是被转变成空字符串。

如上面所说的,直接把 array,object 或 resource 转换成 string 不会得到除了其类型之外的任何有用信息。可以使用函数 print_r() 和 var_dump() 列出这些类型的内容。

大部分的 PHP 值可以转变成 string 来永久保存,这被称作串行化,可以用函数 serialize() 来实现。如果 PHP 引擎设定支持 WDDX,PHP 值也可被串行化为格式良好的 XML 文本。

字符串转换为数值

当一个字符串被当作一个数值来取值,其结果和类型如下:

如果该字符串没有包含 '.','e' 或 'E' 并且其数字值在整型的范围之内(由 PHP_INT_MAX 所定义),该字符串将被当成 integer 来取值。其它所有情况下都被作为 float 来取值。

该字符串的开始部分决定了它的值。如果该字符串以合法的数值开始,则使用该数值。否则其值为 0(零)。合法数值由可选的正负号,后面跟着一个或多个数字(可能有小数点),再跟着可选的指数部分。指数部分由 'e' 或 'E' 后面跟着一个或多个数字构成。

<?php
$foo = 1 + "10.5";                // $foo is float (11.5)
$foo = 1 + "-1.3e3";              // $foo is float (-1299)
$foo = 1 + "bob-1.3e3";           // $foo is integer (1)
$foo = 1 + "bob3";                // $foo is integer (1)
$foo = 1 + "10 Small Pigs";       // $foo is integer (11)
$foo = 4 + "10.2 Little Piggies"; // $foo is float (14.2)
$foo = "10.0 pigs " + 1;          // $foo is float (11)
$foo = "10.0 pigs " + 1.0;        // $foo is float (11)
?>

更多信息可以参考 Unix 手册中的 strtod(3)。

本节中的示例可以通过复制/粘贴到下面的代码中来显示:

b2dbd3a1ec34b59a52e4fcc6fac73376\n";

?>

Don’t think of converting a character to an integer to get its code, like in C. Use the functions ord() and chr() to convert between ASCII codes and characters.

Detailed explanation of string type

The implementation of string in PHP is an array composed of bytes plus an integer indicating the buffer length. There is no information on how to convert bytes into characters, it is up to the programmer to decide. There are no restrictions on what values ​​a string consists of; in particular, bytes whose value is 0 ("NUL bytes") can be anywhere in the string (although there are several functions, referred to in this manual as non-binary "Safe", may ignore all data after NUL bytes).

This feature of the string type explains why there is no separate "byte" type in PHP - strings have been used instead. Functions that return non-text values ​​- such as arbitrary data read from a network socket - will still return strings.

Since PHP does not specify the encoding of the string, how is the string encoded? For example, is the string "á" equal to "xE1" (ISO-8859-1), "xC3xA1" (UTF-8, C form), "x61xCCx81" (UTF-8, D form) or any other possible expression? ? The answer is that the string will be encoded in the same encoding as the script file. So if a script is encoded as ISO-8859-1, the strings in it will also be encoded as ISO-8859-1, and so on. However, this does not apply when Zend Multibyte is activated; in this case the script can be encoded in any way (either explicitly specified or automatically detected) and then converted to some internal encoding, and the string will be encoded in this way. Note that there are some constraints on the encoding of the script (or its internal encoding if Zend Multibyte is activated) - this means that this encoding should be a compatible superset of ASCII, such as UTF-8 or ISO-8859-1. Be aware, however, that state-dependent encodings where the same byte value can be used for both initial and non-initial characters can cause problems when switching states.

Of course, to be useful, functions that operate on text must make assumptions about how the string is encoded. Unfortunately, there are many variations on PHP's functions for this:

Some functions assume that the string is encoded in single bytes, but don't need to interpret the bytes as specific characters. For example substr(), strpos(), strlen() and strcmp(). Another way to think about these functions is that they operate on memory buffers, i.e. in terms of bytes and byte subscripts.

Some functions are passed the encoding method of the string, and may also assume that this information is not available by default. Examples include htmlentities() and most functions in the mbstring extension.

Other functions use the current locale (see setlocale()), but operate byte by byte. For example strcasecmp(), strtoupper() and ucfirst(). This means that these functions can only be used with single-byte encodings, and the encoding must match the locale. For example, strtoupper("á") returns "Á" if the locale is correct and á is a single-byte encoding. If it is encoded in UTF-8, it will not return the correct result, and the result may return a corrupted value depending on the current locale.

Finally some functions will assume that the string is in a specific encoding, usually UTF-8. This is true for most functions in the intl extension and the PCRE (in the above example only when the u modifier is used) extension. Although this is due to its specific purpose, utf8_decode() assumes UTF-8 encoding and utf8_encode() assumes ISO-8859-1 encoding.

Finally, writing programs that use Unicode correctly relies on being careful to avoid functions that might corrupt data. To use functions from the intl and mbstring extensions. But using functions that can handle Unicode encodings is just the beginning. Regardless of the functions provided by any language, the most basic thing is to understand the Unicode specification. For example, a program that assumes only uppercase and lowercase characters would be completely wrong.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn