溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

php中有哪些常用的字符串String函數(shù)

發(fā)布時(shí)間:2021-02-24 15:25:34 來源:億速云 閱讀:133 作者:戴恩恩 欄目:開發(fā)技術(shù)

這篇文章主要介紹了php中有哪些常用的字符串String函數(shù),億速云小編覺得不錯(cuò),現(xiàn)在分享給大家,也給大家做個(gè)參考,一起跟隨億速云小編來看看吧!

php有什么用

php是一個(gè)嵌套的縮寫名稱,指的是英文超級(jí)文本預(yù)處理語言(php:Hypertext Preprocessor)的縮寫,它的語法混合了C、Java、Perl以及php自創(chuàng)新的語法,主要用來做網(wǎng)站開發(fā),許多小型網(wǎng)站都用php開發(fā),因?yàn)閜hp是開源的,從而使得php經(jīng)久不衰。

nl2br

功能:化換行符為<br>

<?php
$str = "cat isn't \n dog";
$result = nl2br($str);
echo $result;
/**結(jié)果
cat isn't
dog
*/

rtrim

功能:清除右邊的空白

<?php
$str = "Hello world ";
echo strlen($str)."<br>";
$result = rtrim($str);
echo strlen($result);
/**結(jié)果
14
11
*/

strip_tags

功能:清除字符串中html和php的標(biāo)記

<?php
$str = "<font color = 'red'>Hello world</font>";
$result = strip_tags($str);
echo $result;
/**結(jié)果
Hello world
*/

strtolower 與 strtoupper

功能:轉(zhuǎn)換成大小寫

<?php
$str = "Hello World!";
$result = strtolower($str);
echo $result."<br>";
$result = strtoupper($str);
echo $result;
/**結(jié)果
hello world!
HELLO WORLD!
*/

trim

功能:去除首尾空格

<?php
$str = " Hello World! ";
$result = trim($str);
echo $str."<br>";
echo $result."<br>";
echo strlen($str)."<br>";
echo strlen($result);
/**結(jié)果
Hello World!
Hello World!
16
12
*/

str_ireplace

功能:替換

<?php
$str = "zhang san";
$result = str_ireplace("zhang","li",$str);
echo $str."<br>";
echo $result;
/**結(jié)果
zhang san
li san
*/

str_repeat

功能:將一個(gè)字符串重復(fù)多遍

<?php
$str = "Hello jiqing!";
$result = str_repeat($str,4);
echo $str."<br>";
echo $result;
/**結(jié)果
Hello jiqing!
Hello jiqing!Hello jiqing!Hello jiqing!Hello jiqing!
*/

str_replace

功能:區(qū)分大小寫的替換

<?php
$str = "hello jiqing!";
$result1 = str_ireplace("Hello","Hi",$str); //不區(qū)分大小寫
$result2 = str_replace("Hello","Hi",$str); //區(qū)分大小寫
echo $str."<br>";
echo $result1."<br>";
echo $result2."<br>";
/**結(jié)果
hello jiqing!
Hi jiqing!
hello jiqing!
*/

str_word_count

功能:返回字符串中單詞的個(gè)數(shù)

<?php
$str = "hello jiqing a!";
$result1 = str_word_count($str); //返回個(gè)數(shù)
$result2 = str_word_count($str,1); //返回?cái)?shù)組
echo $str."<br>";
echo $result1."<br>";
print_r($result2);
/**結(jié)果
hello jiqing a!
3
Array ( [0] => hello [1] => jiqing [2] => a )
*/

strlen

功能:返回字符串長度

<?php
$str = "hello jiqing a!";
$result = strlen($str);
echo $result;
/**結(jié)果
15
*/

substr_count

功能:計(jì)算一個(gè)字符串在另一個(gè)字符串中的個(gè)數(shù)

<?php
$str = "hello jiqing ,hello jim!";
$result = substr_count($str,"hello");
echo $result;
/**結(jié)果
2
*/

substr_replace

功能:從某個(gè)位置開始替換

<?php
$str = "hello jiqing ,hello jim!";
$result = substr_replace($str,"zhangsan",6);
echo $result."<br>";
$result = substr_replace($str,"zhangsan",6,6);//從某個(gè)位置替換,替換幾個(gè)字符串
echo $result;
/**結(jié)果
hello zhangsan
hello zhangsan ,hello jim!
*/

substr

功能:獲取子字符串

<?php
$str = "abcdef";
$result = substr($str,0,1); //從第0個(gè)開始,獲取1個(gè)
echo $result."<br>";
$result = substr($str,0,-1);//從第0個(gè)開始,獲取到除了最后一個(gè)的字符串
echo $result."<br>";
$result = substr($str,2,-1);//從第2個(gè)開始,獲取到除了最后一個(gè)的字符串
echo $result."<br>";
$result = substr($str,-3,-1);//從第-3個(gè)開始,獲取到除了最后一個(gè)的字符串
echo $result."<br>";
$result = substr($str,-3,1);//從第-3個(gè)開始,獲取到除了最后一個(gè)的字符串
echo $result."<br>";
/**結(jié)果
a
abcde
cde
de
d
*/

implode

功能:將數(shù)組轉(zhuǎn)化為字符串

<?php
$array = array("2013","6","3");
$date = implode("/",$array);
echo $date;
/**結(jié)果
2013/6/3
*/

md5

功能:對(duì)字符串進(jìn)行md5加密

<?php
$str = "Hello world";
$result = md5($str);
echo $result;
/**結(jié)果
3e25960a79dbc69b674cd4ec67a72c62
*/

以上就是億速云小編為大家收集整理的php中有哪些常用的字符串String函數(shù),如何覺得億速云網(wǎng)站的內(nèi)容還不錯(cuò),歡迎將億速云網(wǎng)站推薦給身邊好友。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI