Summary of Common String Operation Functions and Usage in PHP
- 2021-11-29 06:33:44
- OfStack
This paper describes the common string operation functions and usage of PHP. Share it for your reference, as follows:
1. Formatting strings
1. Formatting strings
trim()
Function to remove the spaces at the beginning and end of a string and return the resulting string by default by the newline and carriage return characters (\ n and\ r), horizontal and vertical tabs (\ t and X0B)
ltrim()
The function removes spaces only at the beginning (left) of a character
rtrim()
Function removes spaces only from the end (right) of the function
2. Format the string for display
① Formatting with HTML:
n12br()
Function
Insert a newline character before the new line (\ n) in the string
<?php
echo nl2br("One line.\nAnother line.");
?>
Results
One line.
Another line.
Formatting strings for printout
printf()
Structure
$s="world");
printf("Hello %s",$s);
3. Change the case of the letters in the string
函数 | 描述 | 使用 $subject=Hello World | 返回值 |
---|---|---|---|
strtoupper() | 将字符串转为大写 | strtoupper($subject ) | HELLO WORLD |
strtolower() | 将字符串转为小写 | strtolower($subject ) | hello world |
ucfirst() | 如果字符串第1个字符是字符,将其转为大写 | ucfirst($subject ) | Hello world |
ucwords() | 将字符串的每个单词的首字母大写 | ucwords($subject ) | Hello World |
2. Connecting and splitting strings with string functions
1. Use the functions explode (), implode (), and join ()
exlpode()
Break up strings into arrays:
<!DOCTYPE html>
<html>
<body>
<?php
$str = "Hello world. I love Shanghai!";
print_r (explode(" ",$str));
?>
</body>
</html>
Results
Array ( [0] = > Hello [1] = > world. [2] = > I [3] = > love [4] = > Shanghai! )
implode()
(
jion()
Yes
implode()
Alias of function)
Combine array elements into strings:
<!DOCTYPE html>
<html>
<body>
<?php
$arr = array('Hello','World!','I','love','Shanghai!');
echo implode(" ",$arr);
?>
</body>
</html>
Results
Hello World! I love Shanghai!
2. Use the strtok () function
strtok()
Function to split a string into smaller strings (tags).
Grammar
strtok(string,split)
参数 | 描述 |
---|---|
string | 必需。规定要分割的字符串。 |
split | 必需。规定1个或多个分割字符。 |
<!DOCTYPE html>
<html>
<body>
<?php
$string = "Hello world. Beautiful day today.";
$token = strtok($string, " ");
while ($token !== false)
{
echo "$token<br>";
$token = strtok(" ");
}
?>
</body>
</html>
Results
Hello
world.
Beautiful
day
today.
3. Use the substr () function
Definition and usage
ltrim()
0
The function returns part 1 of a string.
Note: length is 0 if the start parameter is negative and length is less than or equal to start.
Grammar
substr(string,start,length)
参数 | 描述 |
---|---|
string | 必需。规定要返回其中1部分的字符串。 |
start | 必需。规定在字符串的何处开始。
|
length | 可选。规定被返回字符串的长度。默认是直到字符串的结尾。
|
<!DOCTYPE html>
<html>
<body>
<?php
echo substr("Hello world",6);
?>
</body>
</html>
Results
world
<!DOCTYPE html>
<html>
<body>
<?php
echo substr("Hello world",10)."<br>";
echo substr("Hello world",1)."<br>";
echo substr("Hello world",3)."<br>";
echo substr("Hello world",7)."<br>";
echo substr("Hello world",-1)."<br>";
echo substr("Hello world",-10)."<br>";
echo substr("Hello world",-8)."<br>";
echo substr("Hello world",-4)."<br>";
?>
</body>
</html>
Results
d
ello world
lo world
orld
d
ello world
lo world
orld
<!DOCTYPE html>
<html>
<body>
<?php
echo substr("Hello world",0,10)."<br>";
echo substr("Hello world",1,8)."<br>";
echo substr("Hello world",0,5)."<br>";
echo substr("Hello world",6,6)."<br>";
echo substr("Hello world",0,-1)."<br>";
echo substr("Hello world",-10,-2)."<br>";
echo substr("Hello world",0,-6)."<br>";
echo substr("Hello world",-2-3)."<br>";
?>
</body>
</html>
Results
Hello worl
ello wor
Hello
world
Hello worl
ello wor
Hello
world
3. String comparison
1. strcmp () compares two strings. If they are equal, the function returns 0
$s="world");
printf("Hello %s",$s);
0
Results
0
2. The strlen () function tests the length of the string
$s="world");
printf("Hello %s",$s);
1
Results
8
For more readers interested in PHP related contents, please check the topics on this site: "Summary of Common Functions and Skills of php", "Summary of Usage of php String (string)", "Encyclopedia of Operation Skills of PHP Array (Array)", "Introduction to Basic Grammar of PHP", "Introduction to Database Operation of php+mysql" and "Summary of Operation Skills of Common Database of php"
I hope this paper is helpful to everyone's PHP programming.