PHP常用字符串操作函數(shù)實例總結(trim、nl2br、addcslashes、uudecode、md5等)
更新時間:2016年01月09日 16:38:44 作者:rogerzhanglijie
這篇文章主要介紹了PHP常用字符串操作函數(shù),結合實例形式總結分析了PHP針對字符串操作的常用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例總結了PHP常用字符串操作函數(shù)。分享給大家供大家參考,具體如下:
/*常用的字符串輸出函數(shù) * * 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/>"; //去除左側的數(shù)字 echo rtrim($str,".")."<br/>"; echo trim($str,"0..9A..Z.")."<br/>"; //去除字符串兩端的大寫字母,數(shù)字還有. //HTML相關的字符串格式化函數(shù) //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函數(shù)相反,將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 ] ) //將一個字符串進行解析,解析成變量和數(shù)組的形式 $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 ] ) //計算兩個字符串的相似度,通過引用方式傳遞第三個參數(shù),similar_text() 將 //計算相似程度百分數(shù)。 $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 ]] ) //統(tǒng)計字符串中單詞的數(shù)量 echo "<hr><br/>"; //int strripos ( string $haystack , string $needle [, int $offset = 0 ] ) //以不區(qū)分大小寫的方式查找指定字符串在目標字符串中最后一次出現(xiàn)的位 //置。與 strrpos() 不同,strripos() 不區(qū)分大小寫。 //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 第一次出現(xiàn)的位置開始到 haystack 結 //尾的字符串。 該函數(shù)區(qū)分大小寫。如果想要不區(qū)分大小寫,請使用 //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中出現(xiàn)的次數(shù),$needle區(qū)分大小寫 $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);
更多關于PHP字符串操作相關內容感興趣的讀者可查看本站專題:《php字符串(string)用法總結》
希望本文所述對大家PHP程序設計有所幫助。
相關文章
escape unescape的php下的實現(xiàn)方法
escape unescape的php下的實現(xiàn)方法...2007-04-04同一空間綁定多個域名而實現(xiàn)訪問不同頁面的PHP代碼
同一空間綁定多個域名而實現(xiàn)訪問不同頁面的PHP代碼...2006-12-12PHP實現(xiàn)二維數(shù)組按照指定的字段進行排序算法示例
這篇文章主要介紹了PHP實現(xiàn)二維數(shù)組按照指定的字段進行排序算法,涉及php針對數(shù)組的遍歷、排序等相關操作技巧,需要的朋友可以參考下2019-04-04