這篇文章主要介紹了PHP常用字符串操作函數,結合實例形式總結分析了PHP針對字符串操作的常用技巧,具有一定參考借鑒價值,需要的朋友可以參考下。
本文實例總結了PHP常用字符串操作函數,分享給大家供大家參考,具體如下:
- /*常用的字符串輸出函數
- *
- * echo() 輸出字符串
- * print() 輸出一個或多個字符串
- * die() 輸出一條信息,并退出當前腳本
- * printf() 輸出格式化字符串
- * sprintf() 把格式化的字符串寫入到一個變量中
- *
- */
- //ucfirst
- //將字符串中的首字母轉換為大寫
- $str="string";
- echo ucfirst($str);
- echo "<hr><br/>";
- //ucwords()
- //將字符串中的每個單詞的首字母大寫
- $ucword="hello everyone!";
- echo ucwords($ucword);
- echo "<hr><br/>";
- //ltrim() rtrim() trim()
- //去除空格
- $str="123 This is a test.....";
- echo ltrim($str,"0..9")."<br/>"; //去除左側的數字
- echo rtrim($str,".")."<br/>";
- echo trim($str,"0..9A..Z.")."<br/>"; //去除字符串兩端的大寫字母,數字還有.
- //HTML相關的字符串格式化函數
- //nl2br()
- //將字符串中的/n轉換為"<br/>"
- $str="this is /n hello world";
- echo nl2br($str).'<br/>';
- //htmlspecialchars()
- //將html標記以字符的形式顯示,不進行解釋
- $str="<b>hello world</b>";
- echo $str."<br/>";
- echo htmlspecialchars($str);
- echo "<hr><br/>";
- //addcslashes
- //添加反斜線
- $str=addcslashes("foo[]","A..z");
- echo $str."<br/>";
- echo addcslashes("zoo['.']",'A..z')."<br/>";
- //convert_uuencode()
- //利用uudecode的方法對字符串進行編碼
- $string="hello world";
- $str= convert_uuencode($string);
- echo $str."<br/>";
- echo convert_uudecode($str)."<br/>";
- //html_entity_decode ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = 'UTF-8' ]] )
- //與htmlentities方法相反,將進行編碼后的html字符轉換為瀏覽器能夠編譯的形式
- $a="I want a bright <b>future</b>";
- $b= htmlentities($a)."<br/>";
- echo $b;
- echo html_entity_decode($b);
- echo "<hr><br/>";
- //htmlspecialchars_decode ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 ] )
- //與htmlspecialchars函數相反,將HTML實體轉換為字符
- $c=htmlspecialchars($a);
- echo $c."<br/>";
- echo htmlspecialchars_decode($c)."<br/>";
- echo "<hr><br/>";
- //lcfirst ( string $str )
- //將字符串的首字符小寫
- $str="Hello World";
- // echo lcfirst($str)."<br/>";
- //md5_file ( string $filename [, bool $raw_output = false ] )
- //對文件進行md5加密
- //
- $string="password";
- $str=md5($string);
- if($str=="5f4dcc3b5aa765d61d8327deb882cf99"){
- echo "The password is right <br/>";
- }
- //parse_str ( string $str [, array &$arr ] )
- //將一個字符串進行解析,解析成變量和數組的形式
- $str = "first=value&arr[]=foo+bar&arr[]=baz";
- parse_str($str,$input);
- print_r($input);
- echo "<hr><br/>";
- //string sha1_file ( string $filename [, bool $raw_output = false ] )
- //計算文件的散列值
- foreach(glob("C:/lamp/appache2/htdocs/*.php") as $ent){
- if(is_dir($ent)){
- continue;
- }
- echo $ent."(SHA1:".sha1_file($ent).")<br/>";
- }
- echo "<hr><br/>";
- //int similar_text ( string $first , string $second [, float &$percent ] )
- //計算兩個字符串的相似度,通過引用方式傳遞第三個參數,similar_text() 將
- //計算相似程度百分數。
- $string1="rogerzhalili";
- $string2="zhangjieroger";
- if(similar_text($string1,$string2,$percent)){
- echo $string1." and ".$string2." has the similarity of:".$percent."<br/>";
- }
- echo "<hr><br/>";
- //string str_shuffle ( string $str )
- //打亂一個字符串
- $string="I want you to solve this problem";
- echo str_shuffle($string)."<br/>";
- //array str_split ( string $string [, int $split_length = 1 ] )
- //按照指定的長度對字符串進行分割
- $arr=str_split($string,3);
- //str_word_count ( string $string [, int $format = 0 [, string $charlist ]] )
- //統計字符串中單詞的數量
- echo "<hr><br/>";
- //int strripos ( string $haystack , string $needle [, int $offset = 0 ] )
- //以不區分大小寫的方式查找指定字符串在目標字符串中最后一次出現的位
- //置。與 strrpos() 不同,strripos() 不區分大小寫。
- //offset用于指定從那個位置開始查找
- $haystack='ababcd';
- $needle='Ab';
- echo "the last".$needle."postion is:".strripos($haystack,$needle)."<br/>";
- echo strrpos($haystack,'ab');
- echo "<hr><br/>";
- //string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] )
- //返回 haystack 字符串從 needle 第一次出現的位置開始到 haystack 結
- //尾的字符串。 該函數區分大小寫。如果想要不區分大小寫,請使用
- //stristr()。
- $a="the First test";
- $needle="Fi";
- echo strstr($a,$needle)."<br/>";
- if($c=strstr($a,"Fio")){
- echo "find".$c."<br/>";
- }
- else
- {
- echo "not find the string!<br/>";
- }
- echo "<hr><br/>";
- //int substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] )
- //查找$needle子字符串在$haystack中出現的次數,$needle區分大小寫
- $hay="la la wa la wa wa lala";
- echo substr_count($hay,"la")."<br>";
- //int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ]]] )
- //正則匹配,將匹配后的結果存放到$matches(如果指定了$matches的話)
- preg_match_all("/?(/d3)?? (?(1) [/-/s] ) /d{3}-/d{4}/x",
- "Call 555-1212 or 1-800-555-1212", $phones);
- echo "<pre>";
- print_r($phones);
- echo "</pre>";
- echo "<hr><br/>";
- //preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
- //搜索subject中匹配pattern的部分, 以replacement進行替換.
- $string = 'April 15, 2003';
- $pattern = '/(/w+) (/d+), (/d+)/i';
- $replacement = '${1}1,$3';
- echo preg_replace($pattern,$replacement,$string);
- echo "<hr><br/>";
- //array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )
- //通過一個正則表達式分隔給定字符串.
- $str = 'string';
- $chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
- print_r($chars);
新聞熱點
疑難解答