VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • PHP常用字符串操作函数实例总结(trim、nl2br、addcslashes、uudecode、md5等)

这篇文章主要介绍了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. } 
  73. //parse_str ( string $str [, array &$arr ] ) 
  74. //将一个字符串进行解析,解析成变量和数组的形式 
  75. $str = "first=value&arr[]=foo+bar&arr[]=baz"; 
  76. parse_str($str,$input); 
  77. print_r($input); 
  78. echo "<hr><br/>"; 
  79. //string sha1_file ( string $filename [, bool $raw_output = false ] ) 
  80. //计算文件的散列值 
  81. foreach(glob("C:/lamp/appache2/htdocs/*.php") as $ent){ 
  82.  if(is_dir($ent)){ 
  83.  continue; 
  84.  } 
  85.  echo $ent."(SHA1:".sha1_file($ent).")<br/>"; 
  86. } 
  87. echo "<hr><br/>"; 
  88. //int similar_text ( string $first , string $second [, float &$percent ] ) 
  89. //计算两个字符串的相似度,通过引用方式传递第三个参数,similar_text() 将 
  90. //计算相似程度百分数。 
  91. $string1="rogerzhalili"; 
  92. $string2="zhangjieroger"; 
  93. if(similar_text($string1,$string2,$percent)){ 
  94.  echo $string1." and ".$string2." has the similarity of:".$percent."<br/>"; 
  95. } 
  96. echo "<hr><br/>"; 
  97. //string str_shuffle ( string $str ) 
  98. //打乱一个字符串 
  99. $string="I want you to solve this problem"; 
  100. echo str_shuffle($string)."<br/>"; 
  101. //array str_split ( string $string [, int $split_length = 1 ] ) 
  102. //按照指定的长度对字符串进行分割 
  103. $arr=str_split($string,3); 
  104. //str_word_count ( string $string [, int $format = 0 [, string $charlist ]] ) 
  105. //统计字符串中单词的数量 
  106. echo "<hr><br/>"; 
  107. //int strripos ( string $haystack , string $needle [, int $offset = 0 ] ) 
  108. //以不区分大小写的方式查找指定字符串在目标字符串中最后一次出现的位 
  109. //置。与 strrpos() 不同,strripos() 不区分大小写。 
  110. //offset用于指定从那个位置开始查找 
  111. $haystack='ababcd'; 
  112. $needle='Ab'; 
  113. echo "the last".$needle."postion is:".strripos($haystack,$needle)."<br/>"; 
  114. echo strrpos($haystack,'ab'); 
  115. echo "<hr><br/>"; 
  116. //string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] ) 
  117. //返回 haystack 字符串从 needle 第一次出现的位置开始到 haystack 结 
  118. //尾的字符串。 该函数区分大小写。如果想要不区分大小写,请使用 
  119. //stristr()。 
  120. $a="the First test"; 
  121. $needle="Fi"; 
  122. echo strstr($a,$needle)."<br/>"; 
  123. if($c=strstr($a,"Fio")){ 
  124.  echo "find".$c."<br/>"; 
  125. } 
  126. else 
  127. { 
  128.  echo "not find the string!<br/>"; 
  129. } 
  130. echo "<hr><br/>"; 
  131. //int substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] ) 
  132. //查找$needle子字符串在$haystack中出现的次数,$needle区分大小写 
  133. $hay="la la wa la wa wa lala"; 
  134. echo substr_count($hay,"la")."<br>"; 
  135. //int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ]]] ) 
  136. //正则匹配,将匹配后的结果存放到$matches(如果指定了$matches的话) 
  137. preg_match_all("/?(\d3)?? (?(1) [\-\s] ) \d{3}-\d{4}/x", 
  138. "Call 555-1212 or 1-800-555-1212", $phones); 
  139. echo "<pre>"; 
  140. print_r($phones); 
  141. echo "</pre>"; 
  142. echo "<hr><br/>"; 
  143. //preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] ) 
  144. //搜索subject中匹配pattern的部分, 以replacement进行替换. 
  145. $string = 'April 15, 2003'; 
  146. $pattern = '/(\w+) (\d+), (\d+)/i'; 
  147. $replacement = '${1}1,$3'; 
  148. echo preg_replace($pattern,$replacement,$string); 
  149. echo "<hr><br/>"; 
  150. //array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] ) 
  151. //通过一个正则表达式分隔给定字符串. 
  152. $str = 'string'; 
 
  1. $chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY); 
  2. print_r($chars);
  3.  

出处:http://www.phpfensi.com/php/20210705/17014.html


相关教程