PHP常見字串操作函式與用法總結

2022-09-26 06:24:09 字數 3242 閱讀 2066

一、字串的格式化

1、字串的格式化

trim()函式可以去除字串的開始位置和結束位置的空格,並將結果字串返回,預設情況下去除的字元是換行符和回車符(\n和\r),水平和垂直製表符(\t和x0b)

ltrim()函式只從字元的開始處(左邊)去除空格

rtrim()函式只從函式的結束處(右邊)去除空格

2、格式化字串以便顯示

①使用html格式化:n12br()函式

在字串中的新行(\n)之前插入換行符

<?php echo nl2br("one line.\nanother line.");

?>

結果one line.

another line.

②為列印輸出而格式化字串

printf()結構

$s="world");

printf("hello %s",$s);

3.改變字串中的字母大小寫

函式描述

使用 $subject=hello world

返回值strtoupper()

將字串轉為大寫

strtoupper($subject )

hello world

strtolower()

將字串轉為小寫

strtolower($subject )

hello world

ucfirst()

如果字串第乙個字元是字元,將其轉為大寫

ucfirst($subject )

hello world

ucwords()

將字串的每個單詞的首字母大寫

ucwords($subject )

hello world

二、用字串函式連線和分割字串

1、用函式explode()、implode()和join()

exlpode()

把字串打散為陣列:

<?php $str = "hello world. i love shanghai!";

print_r (explode(" ",$str));

?>

結果array ( [0] => hello [1] => world. [2] => i [3] => love [4] => shanghai! )

implode()(jion()是implode()函式的別名)

把陣列元素組合為字串:

<?php $arr = array('hello','world!','i','love','shanghai!');

echo implode(" ",$arr);

?>

結果hello world! i love shanghai!

2、使用strtok()函式

strtok()函式把字串分割為更小的字串(標記)。

語法strtok(string,split)

引數描述

string

必需。規定要分割的字串。

split

必需。規定乙個或多個分割字元。

<?php $string = "hello world. beautiful day today.";

$token = strtok($string, " ");

while ($token !== false)

?>

結果hello

world.

beautiful

daytoday.

3、使用substr()函式

定義和用法

substr()程式設計客棧函式返回字串的一部分。

注釋:如果 start 引數是負數且 length 小於或等於 start,則 length 為 0。

語法substr(string,start,length)

引數描述

string

必需。規定要返回其中一部分的字串。

start

必需。規定在字串的何處開始。

length

可選。規定被返回字串的長度。預設是直到字串的結尾。

<?php echo substr("hello world",6);

?>

結果world

<?php echo substr("hello world",10)."

";echo substr("hello world",1)."

";echo substr("hello world",3)."

";echo substr("hello world",7)."

";echo substr("hello world",-1)."

";echo substr("hello world",-10)."

";echo substr("hello world",-8)."

";echo gslxdxcfsubstr("hello world",-4)."

";?>

結果dello world

lo world

orld

dello world

lo world

orld

<?php echo substr("hello world",0,10)."

";echo substr("hello world",1,8)."

";echo substr("hello world",0,5)."

";echo substr("hello world",6,6)."

";echo substr("hello world",0,-1)."

";echo substr("hello world",-10,-2)."

";echo substr("hello world",0,-6)."

";echo substr("hello world",-2-3)."

";?>

結果hello worl

ello wor

hello

world

hello worl

ello wor

hello

world

三、字串的比較

1、strcmp()比較兩個字串,如果相等,函式返回0

<?php echo strcmp("hello world!","hello w程式設計客棧orld!");

?>

gslxdxcf>

結果2、strlen()函式測試字串的長度

<?php echo strlen("shanghai");

?>

python常見字串處理函式與用法彙總

1 find 作用 在乙個較長字串中查詢子串。返回子串所在位置的最左端索引,如果沒有找到則返回 1.如果指定 beg 開始 和 end 結束 範圍,則檢查是否包含在指定範圍內,如果包含子字串返回開始的索引值,否則返回 1。用法 string.find 例項 a i am a boy with no ...

python常見字串操作

str hello world print str.upper 把所有字元中的小寫字母轉換成大寫字母 print str.lower 把所有字元中的大寫字母轉換成小寫字母 print str.capitalize 把第乙個字母轉化為大寫字母,其餘小寫 print str.title 把每個單詞的第乙...

python常見字串操作

附 python2.x和python3.x中raw input 和input 區別 備註 1 在python2.x中raw input 和input 兩個函式都存在,其中區別為 raw input 將所有輸入作為字串看待,返回字串型別 input 只能接收 數字 的輸入,在對待純數字輸入時具有自己的...