php之常用字串方法

來源:互聯網
上載者:User

標籤:row   反轉字串   other   字母   equal   localhost   需求   htm   第一個   

1.trim:去除字串首尾出的空白字元(或其他字元)
eg:
$text = "            there are a few words.       ";
print_r(trim($text));
2.ltrim:去除左邊的空白字元.

3.rtrim:去除右邊的空白字元.
4.str_pad():按需求對字串進行填充.第一個參數是要填充的字串,第二個是填充長度,第三個參數是填充的符號,
第四個參數是填充的方向(
STR_PAD_LEFT:字串左添補.
STR_PAD_RIGHT:字串右添補.
STR_PAD_BOTH:字串兩端添補
)
eg:
$input = "Alice.";

echo str_pad($input,10,"-="); //Alice.-=-=
echo str_pad($input,15,"**",STR_PAD_BOTH);  //****Alice.*****
5.strtolower():將字串str全部變小寫字串.
6.strtoupper():本函數將字串str全部變大寫字串.

7.ucfirst():字串第一個字元改大寫.
8.ucwords():字串每個字第一個字母改大寫.
9.nl2br():將字串"\n"轉成HTML分行符號"<br/>";第二個參數表示是否使用XHTML相容分行符號
eg:
echo nl2br("welcome\r\nThis is my HTML document",false);//welcome
This is my HTML document
10.htmlspecialchars():把指定特殊符號轉成實體,如&lt;&gt;等等
eg:
echo htmlspecialchars("<a href=‘#‘>Test</a>");//<a href=‘#‘>Test</a>
11.strip_tags():從字串中去除HTML和PHP標記.
eg:
$text1 = "<p>Test paragraph.</p><!-- Comment --><a href=‘#‘>Other text</a>";
echo $text1;//Test paragraph.
               Other text


12.strrev():將字串前後顛倒.反轉字串.
$text2 = "hello world";
echo strrev($text2);//dlrow olleh


13.strlen():擷取字串長度.

14.number_format():以千位分隔字元方式格式化一個數字
eg:
$number = 1234.56;
echo number_format($number);//1,235
echo  number_format($number,2);//1,234.56
echo  number_format($number,2,",","");//1234,56
15.md5():加密函數
16.strcmp():二進位安全字串比較
eg:
$var1 = "hello";
$var2 = "Hello";
if(strcmp($var1,$var2)!== 0){
echo "$var1 is not equal to $var2 in a case sensitive string comarison";
}
17.strncmp():二進位安全比較字串開頭的若干個字元;與strcmp相似,不同在於可以指定兩個字串比較時使用的長度.

eg:
$arr1 = $arr2 = array("img12.png","img10.png","img2.png","img1.png");

usort($arr1,"strcmp");//Array ( [0] => img1.png [1] => img10.png [2] => img12.png [3] => img2.png ) 
18.strcasecmp():二進位安全比較字串(不區分大小寫)
19.strnatcmp():使用自然排序演算法比較字串
eg:
$arr1 = $arr2 = array("img12.png","img10.png","img2.png","img1.png");

usort($arr2,"strnatcmp");//Array ( [0] => img1.png [1] => img2.png [2] => img10.png [3] => img12.png )
20.similar_text():計算兩個字串的相似性


php之常用字串方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.