当前位置:网站首页>Introduction notes to PHP zero Foundation (11): String
Introduction notes to PHP zero Foundation (11): String
2022-04-22 20:12:00 【Game programming】
character string String
String type
- Single quote string
Double quote string
nowdoc character string
heredoc character string Example
<?php// 1、 Single quote string $str1 = 'hello world';var_dump($str1);// string(11) "hello world"// 2、 Double quote string $str2 = "hello world";var_dump($str2);// string(11) "hello world"// 3、nowdoc$str2 = <<<'EOD'hello worldEOD;var_dump($str2);// string(11) "hello world"// 4、heredoc$str1 = <<<EODhello worldEOD;var_dump($str1);// string(11) "hello world"
String escape
The system will deal with : The backslash + Letter
for example :
\r\n Carriage returns
PHP Escape characters commonly used in
\' Display single quotation marks in single quotation string \" Display double quotes in a double quote string \r enter ( Go back to the beginning of the line )\n Line break \t tab\$ $ stay PHP As a variable symbol
The difference between single quotation mark and double quotation mark
1、 You can identify... In single quotation marks \' , And you can't recognize it in double quotation marks \'
<?php$str1 = 'hello \'world';$str2 = "hello \'world";var_dump($str1);// string(12) "hello 'world"var_dump($str2);// string(13) "hello \'world"
2、 Variable not recognized in single quotation marks , And double quotation marks can identify $ , Parse variables
- Variables in this province need to be distinguished from the following contents , Ensure variable independence
Use variable identifiers to distinguish , Add a set of braces
{}
<?php$a = 'Tom';$str1 = 'hello $a world';var_dump($str1);// string(14) "hello $a world"$str2 = "hello $a world";var_dump($str2);// string(15) "hello Tom world"$str3 = "hello $aworld";var_dump($str3);// PHP Notice: Undefined variable: aworld$str4 = "hello {$a}world";var_dump($str4);// string(14) "hello Tomworld"
Rules for structurally defining string variables
The boundary character corresponding to the structured definition string is conditional
- There cannot be anything after the upper delimiter
- The bottom border character must be the leftmost top grid
- The lower border character can only be followed by the symbol , Can't follow any characters
- Structurally define the interior of a string ( Between boundary symbols ) All the content of is the string itself
<?php$str1 = <<<EODhello worldEOD;var_dump($str1);// string(11) "hello world"
String length
strlen(string $string): int
Example
<?php$str1 = 'hello world';$str2 = ' Hello world '; // In Chinese utf8 Character set lower occupation 3 Bytes var_dump(strlen($str1)); // int(11)var_dump(strlen($str2)); // int(12)
Multi byte string extension module mbstring(Multi String)
<?php$str1 = 'hello world';$str2 = ' Hello world ';var_dump(mb_strlen($str1)); // int(11)var_dump(mb_strlen($str2)); // int(4)
String correlation function
1、 Conversion function
implode — Converts the value of a one-dimensional array to a string
implode(string $glue, array $pieces): stringimplode(array $pieces): stringprint_r(implode('|', [' Beijing ', ' Shanghai ', ' Guangzhou ']));// Beijing | Shanghai | Guangzhou
explode — Use one string to split another string
explode(string $separator, string $string, int $limit = PHP_INT_MAX): arrayprint_r(explode('|', ' Beijing | Shanghai | Guangzhou '));// Array// (// [0] => Beijing // [1] => Shanghai // [2] => Guangzhou // )
str_split — Convert string to array
str_split(string $string, int $split_length = 1): array// Be careful : There is a problem with Chinese character splitting print_r(str_split('helloworld', 5));// Array// (// [0] => hello// [1] => world// )
2、 Intercept function
trim — Remove white space at the beginning and end of a string ( Or other characters )
trim(string $str, string $character_mask = " \t\n\r\0\x0B"): string
ltrim — Remove the white space at the beginning of the string ( Or other characters )
ltrim(string $str, string $character_mask = ?): string
rtrim — Remove the white space at the end of the string ( Or other characters )
rtrim(string $str, string $character_mask = ?): string
Example
<?phpvar_dump(trim(' Hello , The world '));// string(15) " Hello , The world "var_dump(ltrim(' Hello , The world '));// string(16) " Hello , The world "var_dump(rtrim(' Hello , The world '));// string(16) " Hello , The world "
substr — Returns a substring of a string
substr(string $string, int $offset, ?int $length = null): string// Be careful : There is a problem with Chinese character interception var_dump(substr('Hello World', 6, 5));// string(5) "World"
strstr — return haystack String from needle The string that first appears from the beginning to the end ( Take the file suffix )
strstr(string $haystack, mixed $needle, bool $before_needle = false): stringvar_dump(strstr('Hello,World', 'W'));// string(5) "World"
3、 toggle case
strtolower — Convert a string to lowercase
strtolower(string $string): string
strtoupper — Convert a string to uppercase
strtoupper(string $string): string
ucfirst — Convert the first letter of a string to uppercase
ucfirst(string $str): string
Example
var_dump(strtolower('Hello, World'));// string(12) "hello, world"var_dump(strtoupper('Hello, World'));// string(12) "HELLO, WORLD"var_dump(ucfirst('hello, world'));// string(12) "Hello, world"
4、 Lookup function
strpos — return needle stay haystack First digit position in .
strpos(string $haystack, mixed $needle, int $offset = 0): intvar_dump(strpos('Hello, World', 'o'));// int(4)
strrpos — Return string haystack in needle The location of the last number .
strrpos(string $haystack, string $needle, int $offset = 0): intvar_dump(strrpos('Hello, World', 'o'));// int(8)
5、 Substitution function
str_replace — Substring replacement , The string or array is subject All of them search All be replace Results after replacement .
str_replace( mixed $search, mixed $replace, mixed $subject, int &$count = ?): mixedvar_dump(str_replace('o', 'A', 'Hello, World'));// string(12) "HellA, WArld"
6、 Format function
printf — Output formatted string
printf(string $format, mixed $args = ?, mixed $... = ?): int
sprintf — Returns a formatted string
sprintf(string $format, mixed ...$values): string
Format parameters :https://www.php.net/manual/zh/function.sprintf.php
// Direct output , Return to digital $ret1 = printf('my name is %s, and year old is %d', 'Tom', 18);// my name is Tom, and year old is 18var_dump($ret1); // int(34)// Return string $ret2 = sprintf('my name is %s, and year old is %d', 'Tom', 18);var_dump($ret2);// string(34) "my name is Tom, and year old is 18"
7、 other
str_repeat — Duplicate a string
str_repeat(string $input, int $multiplier): stringecho str_repeat('ABC', 5);// ABCABCABCABCABC
str_shuffle — Randomly scramble a string
str_shuffle(string $str): stringecho str_shuffle('ABCDEFG');// GBEACDF
author : Peng Shiyu
Game programming ️, A game development favorite ~
If the picture is not displayed for a long time , Please use Chrome Kernel browser .
版权声明
本文为[Game programming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204222009008544.html
边栏推荐
- 为什么我建议你从事技术岗,而非文职,销售
- New generation agent weapon - traefik
- 金仓数据库KingbaseES服务启动方法
- Selenium automatic pop-up processing
- <山东大学项目实训>辐射预计算渲染及后处理降噪系统研究(一)
- Acrobat Pro DC 教程,如何使用密码保护 PDF 文件?
- Go realizes Luhn verification of bank card
- 《PyTorch深度学习实践》08 加载数据集
- [eight part essay] usage scenarios and features of JUC
- What if I don't understand? Google's 540 billion parameter new model can explain the laugh point and guess the movie through Emoji expression
猜你喜欢

New generation agent weapon - traefik

判断是否发生塑性变形的条件:von Mises屈服准则

Obtain the real IP address of the client after envoy proxy

Module 4 operation

DNS resource records detailed explanation & authority | recursive resolution difference (super detailed)

Industry trends are far more important than efforts -- top test technology summary

Detailed tutorial on installing MySQL 8.0 + eclipse configuration under Linux

Advanced IPC - DBUS details

运维(33) CentOS7.6通过Kubeadm部署Kubernetes集群

Micro diary: Those seemingly insignificant details and experiences
随机推荐
金仓数据库KingbaseES之null和“ ”的区别
隔壁的wifi,我秒破
Pytorch deep learning practice 09 multi classification questions
Baidu has been listed in the delisting list of the United States, ICLR 2022 outstanding papers have been published, Tsinghua quantum computing startup has surfaced, and more big news is here today
微服务笔记合集
Embedded Web project (I) -- introduction of web server
Micro diary: Those seemingly insignificant details and experiences
微日记:那些看起来并不起眼的细节体验
什么是浏览器同源策略?
envoy代理后获取客户端真实IP
Don't say, "I don't like development, so I choose testing."
C# Process运行cmd命令的异步回显
Hard work alone is not enough! And this
Kingbasees service startup method of Jincang database
《PyTorch深度学习实践》08 加载数据集
文件上传问题记录
An error occurs when starting Kingbase stand-alone service: invalid value for parmeter "bindpuplist": "0-95"
396. Rotation function (mathematical law + iterative method)
Biography: Ali Dharma Institute layoffs 30%! Internal staff: rumors! Netizen: why not cut 29.99%...
What is browser homology policy?