当前位置:网站首页>PHP string operation
PHP string operation
2022-07-27 19:04:00 【Miracle Fan】
String related operations
List of articles
1. Single and double quotation marks define the string
The contents of double quotation marks are PHP The analytic result is ; Single quotation marks are WYSIWYG
<?php
$test="PHP";
$str1="I Like $test";
$str2='I Like $test';
echo $str1;
echo "<br>";
echo $str2;
?>
//I Like PHP
//I Like $test
2. String manipulation
2.1 Remove leading and trailing spaces and special characters
(1)trim() function
Used to remove white space characters at the beginning and end of a string ( Or other characters )
trim(string str,string charlist)
str String object for operation ,charlist Is an optional parameter , Characters you want to filter . If you don't specify the character , Then the following characters are taken by default .
| Parameter values | explain |
|---|---|
| \0 | null |
| \t | tab, tabs |
| \n | A newline |
| \x0B | Vertical tabs |
| \r | A carriage return |
| “ ” | Space |
(2)ltrim() function
Remove the characters on the left side of the string
ltrim(string str,string charlist)
(2)rtrim() function
Remove the characters on the right side of the string
rtrim(string str,string charlist)
<?php
$str='$I Like PHP.html$';
echo trim($str,'$');
echo"<br>";
echo ltrim($str,'$');
echo"<br>";
echo rtrim($str,'$');
?>
<!--I Like PHP.html
I Like PHP.html$
$I Like PHP.html-->
2.2 Gets the length of the string
strlen(string str)
Output the specified string length
<?php
$str2='I Like PHP';
echo strlen($str2);
?>
//12
notes It means {\color{Red} Be careful } notes It means : Chinese characters take up two characters , Others account for 1 Characters
2.3 Intercepting string
substr(string str,int start,int length)
| Parameters | explain |
|---|---|
| str | Specify string object |
| start | Specify where to start intercepting the string . if start It's a negative number , Then intercept from the end |
| length | Specify the number of intercepted strings , It's a negative number , Then the penultimate length character string |
<?php
$str='ILikePHP';
echo substr($str,0);
echo"<br>";
echo substr($str,1,7);
echo"<br>";
echo substr($str,-4,4);
?>
//ILikePHP
//LikePHP
//ePHP
2.4 Retrieve string
1.strstr() function
Gets the substring from the first occurrence of the specified string in another string to the end of the latter
strstr(string haystack,string needle)
| Parameters | explain |
|---|---|
| haystack | Necessary parameters , Specified string |
| needle | Specify the characters , If it is a numerical value, it corresponds to ASCII Value matches character |
<?php
$str='ILikePHP.html';
echo strstr($str,'.');
?>
//.html Get file suffix
2. substr_count() function
Returns the number of occurrences of the retrieval string
substr_count(string haystack,string needle)
<?php
$str='ILikePHP.html';
echo substr_count($str,'P');
?>
//2
2.5 Replace string
Implement the replacement of the specified string
1.str_ireplace() function
Replace the original string with the new substring. The string specified to be replaced
str_ireplace(mixed search,mixed replace,mixed subject,int count)
| Parameters | explain |
|---|---|
| search | Value to search , It can be used array Provide multiple values |
| replace | Specify the replacement value |
| subject | String or array searched and replaced |
| count | Optional parameters , Number of replacements performed |
<?php
$str1="P";
$str2="*";
$str='I Like PHP.html';
echo str_ireplace($str1,$str2,$str);
?>
<!--I Like *H*.html-->
2. substr_replace() function
substr_replace(mixed string,mixed repl,mixed start,mixed length)
<?php
$str1="***";
$str='I Like PHP.html';
$count = 3;
echo substr_replace($str,$str1,7,$count);
?>
<!--I Like ***.html-->
2.6 Division 、 Composite string
1. explode() function
explode(string delimiter,string str ,int limit)
<?php
$str='I Like PHP.html';
$str_arr=explode(' ',$str);
print_r($str_arr);
echo "<br>";
echo $str_arr[0];
echo "<br>";
echo $str_arr[1];
echo "<br>";
echo $str_arr[2];
?>
<!--Array ( [0] => I [1] => Like [2] => PHP.html )
I
Like
PHP.html-->
2. implode() function
implode (string glue,array pieces)
<?php
$str='I Like PHP.html';
$str_arr=explode(' ',$str);
print_r($str_arr);
echo"<br>";
echo implode(" ",$str_arr)
?>
<!--Array ( [0] => I [1] => Like [2] => PHP.html )
I Like PHP.html-->
边栏推荐
- Ridis command notes
- JDBC MySQL 02 data access and Dao mode
- NPM's ID card and dependence
- [wechat applet] project practice - lottery application
- Resource for NS2 beginner
- USB rechargeable hand warmer chip dltap602sc Jericho
- Uploading and downloading of files
- 正则表达式的扩展
- MySQL 05 存储过程
- 自控原理学习笔记-系统稳定性分析(2)-环路分析及Nyquist-Bode判据
猜你喜欢
随机推荐
MySQL 06 transaction, view, index, backup and recovery
MySQL 02 initial experience
`this.$ Emit ` the child component passes multiple parameters to the parent component
JDBC MySQL 02 data access and Dao mode
地图找房的实例
How to send external mail to the company mailbox server on the Intranet
Intelligent insomnia therapeutic instrument product dlt8p68sa Jericho
Typescript installation
npm 基本使用
express
WSN Journal indexed by SCI(转)
Hash、Set、List、Zset、BitMap、Scan
大冤种们,新进测试行业,如何正确选择意向企业?
订单超时取消 及 按类别查询商品
Blog Garden beautification tutorial
Leetcode brushes questions the next day
Led with fan eye protection learning table lamp touch chip-dlt8s12a
MySQL 05 存储过程
Unity-显示Kinect深度数据
Product recommendation and classified product recommendation









