Php 简明教程
PHP - Strings
字符串是一系列字符,如“PHP 支持字符串操作”。PHP 中的字符串是一个字节数组和一个指示缓冲区长度的整数。在 PHP 中,字符与字节相同。这意味着 PHP 仅支持 256 个字符集,因此不提供本机的 Unicode 支持。
A string is a sequence of characters, like 'PHP supports string operations.' A string in PHP as an array of bytes and an integer indicating the length of the buffer. In PHP, a character is the same as a byte. This means that PHP only supports a 256-character set, and hence does not offer native Unicode support.
PHP 支持单引号和双引号的字符串格式。两种格式“this is a simple string”和“this is a simple string”均有效。PHP 还有 Heredoc 和 Nowdoc 形式的字符串数据类型。
PHP supports single quoted as well as double quoted string formation. Both the representations 'this is a simple string' as well as "this is a simple string" are valid. PHP also has Heredoc and Newdoc representations of string data type.
Single-Quoted String
用单引号(字符“'”)引起来的一系列字符是一个字符串。
A sequence of characters enclosed in single quotes (the character ') is a string.
$str = 'this is a simple string';
Example
如果要包含一个文字单引号,请用反斜杠(\)对其进行转义。
If you want to include a literal single quote, escape it with a backslash (\).
<?php
$str = 'This is a \'simple\' string';
echo $str;
?>
它将给出以下内容:
It will give you the following output −
This is a 'simple' string
Example
要指定一个文字反斜杠,请将其加倍(\\)。
To specify a literal backslash, double it (\\).
<?php
$str = 'The command C:\\*.* will delete all files.';
echo $str;
?>
以下是它的 @{s0}
−
Here is its output −
The command C:\*.* will delete all files.
Example
诸如“\r”或“\n”之类的转义序列将被视为文本,并且不会解释它们的特殊含义。如果变量出现在单引号字符串中,它们也不会扩展。
The escape sequences such as "\r" or "\n" will be treated literally and their special meaning will not be interpreted. The variables too will not be expanded if they appear in a single quoted string.
<?php
$str = 'This will not expand: \n a newline';
echo $str . PHP_EOL;
$x=100;
$str = 'Value of x = $x';
echo $str;
?>
它将生成以下 output −
It will produce the following output −
This will not expand: \n a newline
Value of x = $x
Double-Quoted String
用双引号(“"”)引起来的一系列字符是另一种字符串表示形式。
A sequence of characters enclosed in double-quotes (" ") is another string representation.
$str = "this is a simple string";
单引号和双引号字符串是等效的,除了它们对转义序列的处理不同。PHP 会解释特殊字符的某些转义序列。例如,“\r”和“\n”。
Single-quoted and double-quoted strings are equivalent except for their treatment of escape sequences. PHP will interpret certain escape sequences for special characters. For example, "\r" and "\n".
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) |
\e |
escape (ESC or 0x1B (27) in ASCII) |
\f |
form feed (FF or 0x0C (12) in ASCII) |
|backslash |
\$ |
dollar sign |
\" |
How to Escape Octal and Hexademical Characters in PHP?
PHP 支持将八进制和十六进制数字转义为其 ASCII 字符。例如,字符 P 的 ASCII 字符为十进制中的 80。十进制中的 80 对应八进制中的 120。同样,十进制中的 80 对应十六进制中的 50。
PHP supports escaping an Octal and a hexadecimal number to its ASCII character. For example, the ASCII character for P is 80 in decimal. 80 in decimal to Octal is 120. Similarly, 80 in decimal to hexadecimal is 50.
要转义八进制字符,使用“\”前缀;要转义十六进制字符,使用“\x”前缀。
To escape an octal character, prefix it with "\"; and to escape a hexadecimal character, prefix it with "\x".
<?php
$str = "\120\110\120";
echo "PHP with Octal: ". $str;
echo PHP_EOL;
$str = "\x50\x48\x50";
echo "PHP with Hexadecimal: ". $str;
?>
检查 output −
Check the output −
PHP with Octal: PHP
PHP with Hexadecimal: PHP
与单引号串一样,转义任何其他字符也将导致反斜杠被打印。双引号串最重要的特征是变量名将被展开。
As in single quoted strings, escaping any other character will result in the backslash being printed too. The most important feature of double-quoted strings is the fact that variable names will be expanded.
Example
PHP 中的双引号串展开变量名(PHP 变量用 $ 符号作为前缀)。如要在 PHP 串中实际表示 “$” 符号,用 “\” 字符进行转义。
A double-quoted string in PHP expands the variable names (PHP variables are prefixed with $ symbol). To actually represent a "$" symbol in a PHP string, escape it by prefixing with the "\" character.
<?php
$price = 200;
echo "Price = \$ $price";
?>
您将获得以下内容 output −
You will get the following output −
Price = $ 200
String Concatenation Operator
要将两个字符串变量连接在一起,PHP 使用点 (.) 运算符 −
To concatenate two string variables together, PHP uses the dot (.) operator −
<?php
$string1="Hello World";
$string2="1234";
echo $string1 . " " . $string2;
?>
在这里,你会得到以下 output −
Here, you will get the following output −
Hello World 1234
在上述示例中,我们使用了连接运算符两次。这是因为我们必须插入第三个串。在两个字符串变量之间,我们添加了一个包含单个字符(一个空格)的串,以分隔这两个变量。
In the above example, we used the concatenation operator twice. This is because we had to insert a third string. Between the two string variables, we added a string with a single character, an empty space, to separate the two variables.
PHP 的标准库包括很多用于字符串处理的函数。它们可以在 PHP 的官方文档中找到 ( https://www.php.net/manual/en/ref.strings.php )。
The standard library of PHP includes many functions for string processing. They can be found at PHP’s official documentation (https://www.php.net/manual/en/ref.strings.php).
The strlen() Function
strlen() 函数用于找到一个字符串的长度。
The strlen() function is used to find the length of a string.
Example
让我们找到字符串 “Hello world!” 的长度 −
Let’s find the length of our string "Hello world!" −
<?php
echo strlen("Hello world!");
?>
它将生成以下 output −
It will produce the following output −
12
字符串的长度通常用于循环或其他函数中,这些时候了解字符串何时结束非常重要(也就是说,在循环中,我们希望在字符串中的最后一个字符后面停止循环)。
The length of a string is often used in loops or other functions, when it is important to know when the string ends (that is, in a loop, we would want to stop the loop after the last character in the string).
The strpos() Function
strpos() 函数用于在字符串内搜索字符串或字符。
The strpos() function is used to search for a string or character within a string.
-
If a match is found in the string, this function will return the position of the first match.
-
If no match is found, it will return FALSE.
Example
让我们看看能否在我们的字符串中找到字符串 “world” −
Let’s see if we can find the string "world" in our string −
<?php
echo strpos("Hello world!","world");
?>
它将生成以下 output −
It will produce the following output −
6
如你所见,字符串 “world” 在我们字符串中的位置是 “6”。这个位置是 “6” 而不是 “7” 的原因是字符串中的第一个位置是 “0”,而不是 “1”。
As you can see, the position of the string "world" in our string is "6". The reason that it is "6", and not "7", is that the first position in the string is "0", and not "1".