国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP常用字符串操作函數實例總結(trim、nl2br、addcslashes、uudecode、md5等)

2024-09-04 11:42:25
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了PHP常用字符串操作函數,結合實例形式總結分析了PHP針對字符串操作的常用技巧,具有一定參考借鑒價值,需要的朋友可以參考下。

本文實例總結了PHP常用字符串操作函數,分享給大家供大家參考,具體如下:

  1. /*常用的字符串輸出函數 
  2. * 
  3. * echo() 輸出字符串 
  4. * print() 輸出一個或多個字符串 
  5. * die() 輸出一條信息,并退出當前腳本 
  6. * printf() 輸出格式化字符串 
  7. * sprintf() 把格式化的字符串寫入到一個變量中 
  8. * 
  9. */ 
  10. //ucfirst  
  11. //將字符串中的首字母轉換為大寫 
  12. $str="string"
  13. echo ucfirst($str); 
  14. echo "<hr><br/>"
  15. //ucwords() 
  16. //將字符串中的每個單詞的首字母大寫 
  17. $ucword="hello everyone!"
  18. echo ucwords($ucword); 
  19. echo "<hr><br/>"
  20. //ltrim() rtrim() trim() 
  21. //去除空格 
  22. $str="123 This is a test....."
  23. echo ltrim($str,"0..9")."<br/>"//去除左側的數字  
  24. echo rtrim($str,".")."<br/>"
  25. echo trim($str,"0..9A..Z.")."<br/>"//去除字符串兩端的大寫字母,數字還有. 
  26. //HTML相關的字符串格式化函數 
  27. //nl2br() 
  28. //將字符串中的/n轉換為"<br/>" 
  29. $str="this is /n hello world"
  30. echo nl2br($str).'<br/>'
  31. //htmlspecialchars() 
  32. //將html標記以字符的形式顯示,不進行解釋 
  33. $str="<b>hello world</b>"
  34. echo $str."<br/>"
  35. echo htmlspecialchars($str); 
  36. echo "<hr><br/>"
  37. //addcslashes 
  38. //添加反斜線 
  39. $str=addcslashes("foo[]","A..z"); 
  40. echo $str."<br/>"
  41. echo addcslashes("zoo['.']",'A..z')."<br/>"
  42. //convert_uuencode() 
  43. //利用uudecode的方法對字符串進行編碼 
  44. $string="hello world"
  45. $str= convert_uuencode($string); 
  46. echo $str."<br/>"
  47. echo convert_uudecode($str)."<br/>"
  48. //html_entity_decode ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = 'UTF-8' ]] ) 
  49. //與htmlentities方法相反,將進行編碼后的html字符轉換為瀏覽器能夠編譯的形式 
  50. $a="I want a bright <b>future</b>"
  51. $b= htmlentities($a)."<br/>"
  52. echo $b
  53. echo html_entity_decode($b); 
  54. echo "<hr><br/>"
  55. //htmlspecialchars_decode ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 ] ) 
  56. //與htmlspecialchars函數相反,將HTML實體轉換為字符 
  57. $c=htmlspecialchars($a); 
  58. echo $c."<br/>"
  59. echo htmlspecialchars_decode($c)."<br/>"
  60. echo "<hr><br/>"
  61. //lcfirst ( string $str ) 
  62. //將字符串的首字符小寫 
  63. $str="Hello World"
  64. // echo lcfirst($str)."<br/>"; 
  65. //md5_file ( string $filename [, bool $raw_output = false ] ) 
  66. //對文件進行md5加密 
  67. // 
  68. $string="password"
  69. $str=md5($string); 
  70. if($str=="5f4dcc3b5aa765d61d8327deb882cf99"){ 
  71.  echo "The password is right <br/>"
  72. //parse_str ( string $str [, array &$arr ] ) 
  73. //將一個字符串進行解析,解析成變量和數組的形式 
  74. $str = "first=value&arr[]=foo+bar&arr[]=baz"
  75. parse_str($str,$input); 
  76. print_r($input); 
  77. echo "<hr><br/>"
  78. //string sha1_file ( string $filename [, bool $raw_output = false ] ) 
  79. //計算文件的散列值 
  80. foreach(glob("C:/lamp/appache2/htdocs/*.php"as $ent){ 
  81.  if(is_dir($ent)){ 
  82.  continue
  83.  } 
  84.  echo $ent."(SHA1:".sha1_file($ent).")<br/>"
  85. echo "<hr><br/>"
  86. //int similar_text ( string $first , string $second [, float &$percent ] ) 
  87. //計算兩個字符串的相似度,通過引用方式傳遞第三個參數,similar_text() 將 
  88. //計算相似程度百分數。 
  89. $string1="rogerzhalili"
  90. $string2="zhangjieroger"
  91. if(similar_text($string1,$string2,$percent)){ 
  92.  echo $string1." and ".$string2." has the similarity of:".$percent."<br/>"
  93. echo "<hr><br/>"
  94. //string str_shuffle ( string $str ) 
  95. //打亂一個字符串 
  96. $string="I want you to solve this problem"
  97. echo str_shuffle($string)."<br/>"
  98. //array str_split ( string $string [, int $split_length = 1 ] ) 
  99. //按照指定的長度對字符串進行分割 
  100. $arr=str_split($string,3); 
  101. //str_word_count ( string $string [, int $format = 0 [, string $charlist ]] ) 
  102. //統計字符串中單詞的數量 
  103. echo "<hr><br/>"
  104. //int strripos ( string $haystack , string $needle [, int $offset = 0 ] ) 
  105. //以不區分大小寫的方式查找指定字符串在目標字符串中最后一次出現的位 
  106. //置。與 strrpos() 不同,strripos() 不區分大小寫。 
  107. //offset用于指定從那個位置開始查找 
  108. $haystack='ababcd'
  109. $needle='Ab'
  110. echo "the last".$needle."postion is:".strripos($haystack,$needle)."<br/>"
  111. echo strrpos($haystack,'ab'); 
  112. echo "<hr><br/>"
  113. //string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] ) 
  114. //返回 haystack 字符串從 needle 第一次出現的位置開始到 haystack 結 
  115. //尾的字符串。 該函數區分大小寫。如果想要不區分大小寫,請使用 
  116. //stristr()。 
  117. $a="the First test"
  118. $needle="Fi"
  119. echo strstr($a,$needle)."<br/>"
  120. if($c=strstr($a,"Fio")){ 
  121.  echo "find".$c."<br/>"
  122. else 
  123.  echo "not find the string!<br/>"
  124. echo "<hr><br/>"
  125. //int substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] ) 
  126. //查找$needle子字符串在$haystack中出現的次數,$needle區分大小寫 
  127. $hay="la la wa la wa wa lala"
  128. echo substr_count($hay,"la")."<br>"
  129. //int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ]]] ) 
  130. //正則匹配,將匹配后的結果存放到$matches(如果指定了$matches的話) 
  131. preg_match_all("/?(/d3)?? (?(1) [/-/s] ) /d{3}-/d{4}/x"
  132. "Call 555-1212 or 1-800-555-1212"$phones); 
  133. echo "<pre>"
  134. print_r($phones); 
  135. echo "</pre>"
  136. echo "<hr><br/>"
  137. //preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] ) 
  138. //搜索subject中匹配pattern的部分, 以replacement進行替換. 
  139. $string = 'April 15, 2003'
  140. $pattern = '/(/w+) (/d+), (/d+)/i'
  141. $replacement = '${1}1,$3'
  142. echo preg_replace($pattern,$replacement,$string); 
  143. echo "<hr><br/>"
  144. //array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] ) 
  145. //通過一個正則表達式分隔給定字符串. 
  146. $str = 'string'
  147. $chars = preg_split('//'$str, -1, PREG_SPLIT_NO_EMPTY); 
  148. print_r($chars);

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 唐山市| 台前县| 汝城县| 嘉善县| 阳东县| 梨树县| 措美县| 郓城县| 平度市| 贵阳市| 平顺县| 光泽县| 贵阳市| 贵港市| 兴山县| 安新县| 中阳县| 南和县| 北海市| 蒙城县| 大余县| 肃北| 常宁市| 七台河市| 涟水县| 合江县| 屏南县| 林周县| 灌阳县| 博兴县| 且末县| 禄丰县| 江城| 突泉县| 铁力市| 清水河县| 衡南县| 喀什市| 阿拉尔市| 酒泉市| 金坛市|