Home > Backend Development > PHP Tutorial > (Practical) Summary of examples of commonly used string functions in PHP [conversion, replacement, calculation, interception, encryption]

(Practical) Summary of examples of commonly used string functions in PHP [conversion, replacement, calculation, interception, encryption]

黄舟
Release: 2023-03-05 12:48:02
Original
1035 people have browsed it

The examples in this article summarize the commonly used string String functions in PHP. Share it with everyone for your reference, the details are as follows:

nl2br

Function: Change line breaks to

<?php
$str = "cat isn&#39;t \n dog";
$result = nl2br($str);
echo $result;
/**结果
cat isn&#39;t
dog
*/
Copy after login

rtrim

Function: Clear The blank space on the right

<?php
$str = "Hello world ";
echo strlen($str)."<br>";
$result = rtrim($str);
echo strlen($result);
/**结果
14
11
*/
Copy after login

strip_tags

Function: Clear the tags of html and php in the string

<?php
$str = "<font color = &#39;red&#39;>Hello world</font>";
$result = strip_tags($str);
echo $result;
/**结果
Hello world
*/
Copy after login

strtolower and strtoupper

Function: Convert to upper and lower case

<?php
$str = "Hello World!";
$result = strtolower($str);
echo $result."<br>";
$result = strtoupper($str);
echo $result;
/**结果
hello world!
HELLO WORLD!
*/
Copy after login

trim

Function: Remove leading and trailing spaces

<?php
$str = " Hello World! ";
$result = trim($str);
echo $str."<br>";
echo $result."<br>";
echo strlen($str)."<br>";
echo strlen($result);
/**结果
Hello World!
Hello World!
16
12
*/
Copy after login

str_ireplace

Function: Replace

<?php
$str = "zhang san";
$result = str_ireplace("zhang","li",$str);
echo $str."<br>";
echo $result;
/**结果
zhang san
li san
*/
Copy after login

str_repeat

Function: Repeat a string multiple times

<?php
$str = "Hello jiqing!";
$result = str_repeat($str,4);
echo $str."<br>";
echo $result;
/**结果
Hello jiqing!
Hello jiqing!Hello jiqing!Hello jiqing!Hello jiqing!
*/
Copy after login

str_replace

Function: Case-sensitive replacement

<?php
$str = "hello jiqing!";
$result1 = str_ireplace("Hello","Hi",$str); //不区分大小写
$result2 = str_replace("Hello","Hi",$str); //区分大小写
echo $str."<br>";
echo $result1."<br>";
echo $result2."<br>";
/**结果
hello jiqing!
Hi jiqing!
hello jiqing!
*/
Copy after login

str_word_count

Function: Return the string Number of words

<?php
$str = "hello jiqing a!";
$result1 = str_word_count($str); //返回个数
$result2 = str_word_count($str,1); //返回数组
echo $str."<br>";
echo $result1."<br>";
print_r($result2);
/**结果
hello jiqing a!
3
Array ( [0] => hello [1] => jiqing [2] => a )
*/
Copy after login

strlen

Function: Return the string length

<?php
$str = "hello jiqing a!";
$result = strlen($str);
echo $result;
/**结果
15
*/
Copy after login

substr_count

Function: Calculate the position of one string in another string Number

<?php
$str = "hello jiqing ,hello jim!";
$result = substr_count($str,"hello");
echo $result;
/**结果
2
*/
Copy after login

substr_replace

Function: Replace from a certain position

<?php
$str = "hello jiqing ,hello jim!";
$result = substr_replace($str,"zhangsan",6);
echo $result."<br>";
$result = substr_replace($str,"zhangsan",6,6);//从某个位置替换,替换几个字符串
echo $result;
/**结果
hello zhangsan
hello zhangsan ,hello jim!
*/
Copy after login

substr

Function: Get substring

<?php
$str = "abcdef";
$result = substr($str,0,1); //从第0个开始,获取1个
echo $result."<br>";
$result = substr($str,0,-1);//从第0个开始,获取到除了最后一个的字符串
echo $result."<br>";
$result = substr($str,2,-1);//从第2个开始,获取到除了最后一个的字符串
echo $result."<br>";
$result = substr($str,-3,-1);//从第-3个开始,获取到除了最后一个的字符串
echo $result."<br>";
$result = substr($str,-3,1);//从第-3个开始,获取到除了最后一个的字符串
echo $result."<br>";
/**结果
a
abcde
cde
de
d
*/
Copy after login

implode

Function: Convert array into string

<?php
$array = array("2016","6","3");
$date = implode("/",$array);
echo $date;
/**结果
2016/6/3
*/
Copy after login

md5

Function: md5 encryption of string

<?php
$str = "Hello world";
$result = md5($str);
echo $result;
/**结果
3e25960a79dbc69b674cd4ec67a72c62
*/
Copy after login

The above is (practical Article) Summary of examples of commonly used string functions in PHP [conversion, replacement, calculation, interception, encryption]. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template