PHP notes 5--string manipulation

String definition syntax

1) Single quoted string: Use single quotes to wrap
2) Double quoted string: Use double quotes to wrap
3) Nowdoc string: Single quoted string without single quotes
$str = <<<'Boundary character'
string content
boundary break;
. 4) heredoc in string: double quotes not double-quoted string
$ str = <<< boundary character
string contents
boundary symbol;

Quotation method: It is more suitable to define strings that are relatively short (no more than one line) or without structure (indentation, etc.). If there are structural requirements, or the content exceeds one line, you can use nowdoc and heredoc string definitions

$a = '123';
$b = "123";
//heredoc
$c = <<<EOD
asdfasdfasdfadfasdfasf
asdf得到
EOD;
//nowdoc
$d = <<<'EOD'
asdfasdfasdfadfasdfasf
asdf得到222
EOD;
echo $c.'<br/>'.$d

Rules for the structured definition of string variables
1. The boundary character corresponding to the structured definition string has conditions:
1) The upper boundary character cannot be followed by any content (including spaces);
2) The lower boundary character must be the top grid: the leftmost;
3) The boundary can only be followed by a semicolon and cannot be followed by any content;
2. All the content inside the structured definition string (between boundary characters) is the string itself

String escaping

The meaning of escape: In the general computer protocol, there are some letters defined in a specific way, and the system will deal with it specifically: usually this way is to use the characteristics of backslash + letter (word):

\r\n:回车换行
PHP在识别转义字符的时候也是使用同样的模式:反斜杠+字母
在PHP中系统常用的转义符号:
\’:在单引号字符串中显示单引号
\”:在双引号字符串中显示双引号
\r:代表回车(理论上是回到当前行的首位置)
\n:代表新一行
\t:类似tab键,输出4个空格
\$:在PHP中使用$符号作为变量符号,因此需要特定识别

The difference between single quotes and double quotes:

1. \'can be recognized in single quotation marks, but \'cannot be recognized in double quotation marks (the browser below shows the source code of the page)
2. Since the $ symbol can be recognized in double quotation marks, variables in double quotation marks can be resolved , And single quotation marks are not allowed
. The rules for variable identification in double quotation marks:
1) The variable itself system can be distinguished from the following content: the independence of the variable should be guaranteed, and the system should not be difficult to distinguish
2) Use the variable professional identifier (distinguish), give Variables plus a set of braces {}

$a = '变量a';
echo $a.'<br/>'; //变量a
$b = 'bbb $a'; 
echo $b.'<br/>';//bbb $a
$c = "a123123$a";
echo $c.'<br/>';//a123123变量a
$d = "dddd$a ddd";
echo $d.'<br/>';//dddd变量a ddd
$e = "dddd $a ddd";
echo $e.'<br/>';//dddd 变量a ddd
$f = "dddd{$a}ddd";
echo $f.'<br/>';// dddd变量addd

String length problem

1) Basic function strlen(): Get the length of the string (bytes as a unit)
2) The length of the multi-byte string: including the length of Chinese.
Let the length of a Chinese be 1 instead of 3
3) Multi-byte string extension module: mbstring extension (mb: Multi Bytes)
First, you need to load the mbstring extension of PHP. You can use the mb extension to bring a lot of functions. The
mbstring extension is about Character statistics:
strlen is only for the standard exchange code ASCII
mbstring will be for different character sets (php7 default character set is utf-8)

$a = '张三';
$b = '123';
echo strlen($a),'<br/>',strlen($b); // 4 3
echo '<hr/>';
echo mb_strlen($a),'<br/>',mb_strlen($b),'<br/>',mb_strlen($a,'utf-8'); // 4 3 2

String related functions:

1) Conversion functions: implode(), explode(), str_split()
implode (connection method, array): connect the elements in the array into a string according to a certain rule
explode (split character, target string): combine the characters The string is split according to a certain format and becomes an array
China|Beijing|Shunyi == array('China','Beijing','Shunyi');
str_split(string, character length): Split the string according to the specified length Array
2) Intercept function: trim(), ltrim(), rtrim()
trim(string[,specified character]): By default, it is used to remove the spaces on both sides of the string (not in the middle), but you can also specify to remove The content of is to remove the content on both sides according to the specified content: until it encounters a character that is not the target character.
ltrim(): remove the left
rtrim(): remove the right
3) interception function: substr(), strstr()
substr (String, starting position starts from 0 [, length]): The string is intercepted at the specified position, and the specified length can be intercepted (not specified to the end)
strstr (string, matching character): Start from the specified position and intercepted to the end (Can be used to take the file suffix)

4) Size conversion function: strtolower(), strtoupper(), ucfirst()
strtolower: all lowercase
strtoupper: all uppercase
ucfirst: first letter uppercase

5) Search function: strpos(), strrpos()
strpos(string, matching character): determine the position of the character in the target string (first time)
strrpos(string, matching character): determine the character in the target string Last position

6) Replacement function: str_replace()
str_replace (matching target, replaced content, string itself): replace part of the string in the target string

7) Formatting functions: printf(), sprintf()
srintf/sprintf (the output string has placeholders, the order of the content...): format the output data

8) Others: str_repeat(), str_shuffle()
str_repeat(): repeat a string N times
str_shuffle(): randomly shuffle the string

$a = ' abc cde sdd ';
echo explode($a,' ');
echo '<br/>';
echo str_split($a,3);
echo '<br/>';
var_dump(trim($a));//string(11) "abc cde sdd"
echo '<br/>';
echo substr($a,3,3);//c c
echo '<br/>';
echo strtoupper($a);//ABC CDE SDD
echo '<br/>';
echo strpos($a,'d');//6
echo '<br/>';
echo  str_replace('a','1',$a);//1bc cde sdd
echo '<br/>';
echo str_repeat('1',5);//11111
echo '<br/>';
echo str_shuffle($a);//s d e cba ddc

Guess you like

Origin blog.csdn.net/zhangxm_qz/article/details/108525419