欢迎来到福编程网,本站提供各种互联网专业知识!

PHP常用字符串操作函数实例总结(trim、nl2br、addcslashes、uudecode、md5等)

发布时间:2016-01-09 作者:佚名 来源:转载
这篇文章主要介绍了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 "

    ";

  15. //ucwords()
  16. //将字符串中的每个单词的首字母大写
  17. $ucword="hello everyone!";
  18. echo ucwords($ucword);
  19. echo "


    ";

  20. //ltrim() rtrim() trim()
  21. //去除空格
  22. $str="123 This is a test.....";
  23. echo ltrim($str,"0..9")."

    "; //去除左侧的数字

  24. echo rtrim($str,".")."

    ";

  25. echo trim($str,"0..9A..Z.")."

    "; //去除字符串两端的大写字母,数字还有.

  26. //HTML相关的字符串格式化函数
  27. //nl2br()
  28. //将字符串中的n转换为"

    "

  29. $str="this is n hello world";
  30. echo nl2br($str).'

    ';

  31. //htmlspecialchars()
  32. //将html标记以字符的形式显示,不进行解释
  33. $str="hello world";
  34. echo $str."

    ";

  35. echo htmlspecialchars($str);
  36. echo "


    ";

  37. //addcslashes
  38. //添加反斜线
  39. $str=addcslashes("foo[]","A..z");
  40. echo $str."

    ";

  41. echo addcslashes("zoo['.']",'A..z')."

    ";

  42. //convert_uuencode()
  43. //利用uudecode的方法对字符串进行编码
  44. $string="hello world";
  45. $str= convert_uuencode($string);
  46. echo $str."

    ";

  47. echo convert_uudecode($str)."

    ";

  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 future";
  51. $b= htmlentities($a)."

    ";

  52. echo $b;
  53. echo html_entity_decode($b);
  54. echo "


    ";

  55. //htmlspecialchars_decode ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 ] )
  56. //与htmlspecialchars函数相反,将HTML实体转换为字符
  57. $c=htmlspecialchars($a);
  58. echo $c."

    ";

  59. echo htmlspecialchars_decode($c)."

    ";

  60. echo "


    ";

  61. //lcfirst ( string $str )
  62. //将字符串的首字符小写
  63. $str="Hello World";
  64. // echo lcfirst($str)."

    ";

  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

    ";

  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 "


    ";

  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).")

    ";

  86. }
  87. echo "


    ";

  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."

    ";

  95. }
  96. echo "


    ";

  97. //string str_shuffle ( string $str )
  98. //打乱一个字符串
  99. $string="I want you to solve this problem";
  100. echo str_shuffle($string)."

    ";

  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 "


    ";

  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)."

    ";

  114. echo strrpos($haystack,'ab');
  115. echo "


    ";

  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)."

    ";

  123. if($c=strstr($a,"Fio")){
  124. echo "find".$c."

    ";

  125. }
  126. else
  127. {
  128. echo "not find the string!

    ";

  129. }
  130. echo "


    ";

  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")."

    ";

  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 "

    代码
    1. ";
    2. print_r($phones);
    3. echo "
    ";
  140. echo "

    ";

  141. //preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
  142. //搜索subject中匹配pattern的部分, 以replacement进行替换.
  143. $string = 'April 15, 2003';
  144. $pattern = '/(w+) (d+), (d+)/i';
  145. $replacement = '${1}1,$3';
  146. echo preg_replace($pattern,$replacement,$string);
  147. echo "


    ";

  148. //array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )
  149. //通过一个正则表达式分隔给定字符串.
  150. $str = 'string';
  151. $chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
  152. print_r($chars);

更多关于PHP字符串操作相关内容感兴趣的读者可查看本站专题:《php字符串(string)用法总结》

希望本文所述对大家PHP程序设计有所帮助。

相关推荐

返回顶部