PHP字符串函数

1.格式化输出函数:

   1.ltrim()                            去除左边空格

   2.rtrim()                            去除右边空格

   3.trim()             去除两边空格

   4.str_pad()           把字符串填充为新函数

   5.strtolower()         把字符串转换为小写

   6.strtoupper()        把字符串转为大写

   7.ucfirst()           把字符串中的首字符转为大写

   8.Ucwords()            把字符串中的每个单词的首字母转为大写

   9.nl2br()               把字符串中的每个新行之前插入HTML实体

   10.htmlentities()         把字符串转为HTML实体

     11.htmlspeciachars()      把一些预定义的字符串转为HTML实体

     12.Strislashes()           删除由addcsashes()函数添加的反斜杠

   13.strip_tags()       剥去HTML XML以及PHP的标签

   14.number_format()     通过千位组来格式化数字

   15.strrev()                    反转字符串

   16.md5()                          将一个字符串MD5计算

2.比较函数:

   1.strcmp()

   2.strcasecmp()               忽略字符串中字母大小写比较

    
3.替换函数:

   1.str_replace($search,$replace,$string,count);

      参数:(string,string,string,count);

         (array,string,string,count);

    $value=array('h','e','l','o');
    $string="Hello world";
    echo str_replace($value,"",$string)."<br>";
    $value=array(a,b,D,0,W,i);
    echo str_ireplace($value,"",$string)."<br>";

         (array,array,string,count);

    $value=array('h','e','l','o');
    $string="Hello world";
    $replace=array('f','u','c','...');
    echo str_replace($value,$replace,$string)."<br>";
    $value=array(a,b,D,0,W,i);
    $replace=array('f','u','c','...');
    echo str_ireplace($value,$replace,$string)."<br>";

   2.str_ireplace()

4.查找函数  

         1.strstr($search,$string)       -------搜索一个字符串在另一个字符串中的首个,有则返回首个之后的所有字符串   (否则返回FALSE)

    2.stristr($search,$string);     -------搜索一个字符串在另一个字符串中的首个字符串,有则返回首个自后的所有字符串 (否则返回FALSE)

    3.strpos($search,$string);     ------搜索一个字符串在另个字符串中的首个位置,有则返回首个字符串的位置  (否则返回FALSE)

    3.strrpos($search,$string);    ------搜索一个字符串在另个字符串中的最后一个位置,有则返回最后一个字符串的位置(否则返回FALSE)

    4.stripos()  strripos()  函数

    5.substr($string,$star,$charlength);   --在字符串string中从star开始有charlength被删除;  

5.字符串分割:

    1.explode($separator,$string,limit);   ------将string字符串以separator 分割  分割limit个

6.字符串组合:

    1.implode($glue,array pieces);          ------ 以glue字符将pieces中的数组组合起来

 7.正则表达式中的匹配查找:

    1.preg_match($pattern,$string,$matches);

      参数:pattern: 正则表达式

        string  :需要匹配的字符串

        matches: 保存与第一个参数中子模式的各个部分的匹配结果。

    2.子模式:

      正则表达式的子模式是使用括号()括起来的模式单元,

      mathces[0] :表示的是pattern中匹配出来的整体内容

      mathces[1]:  表示的是pattern中匹配出来的第一个()的内容

      mathces[2]:  表示的是pattern中匹配出来的第二个()的内容

    3.preg_match_all($pattern,$string,$mathces)

      该函数将所有可能的匹配结果放入第三个参数中的数组里

    4.preg_grep($pattern,$array)

      参数:pattern正则表达式

         array  :需要匹配的数组:

    5.preg_replace($pattern,$replacement,$subject)

      参数:$pattern  正则表达式

         $replacement  替换的文字

         $subject  需要替换的文字

      1.普通:

    $text="今年的国庆节假日是10/01/2014到10/01/2014";
    $pattern="/(d+)/(d+)/(d+)/";
    echo preg_replace($pattern,"asdf",$text);
今年的国庆节假日是asdf到asdf

      2.反向引用:

    $text="今年的国庆节假日是10/01/2014到10/01/2014";
    $pattern="/(d+)/(d+)/(d+)/";
    echo preg_replace($pattern,"${3}-${2}-${1}",$text)."<br>";
    echo preg_replace($pattern,"\3-\2-\1",$text);
今年的国庆节假日是2014-01-10到2014-01-10
今年的国庆节假日是2014-01-10到2014-01-10

       3.模式修正符”e“

$text="这个文本中有<b>粗体</b>和<u>带有下划线</u>以及<i>斜体</i>还有<font color='red' size=7>带有颜色和字体大小</font>的标记";
$pattern="/(</?)(w+)([^>]*>)/e";
echo preg_replace($pattern,"'${1}'.strtoupper('${2}').'${3}'",$text)."<br>";
这个文本中有<B>粗体</B>和<U>带有下划线</U>以及<I>斜体</I>还有<FONT color='red' size=7>带有颜色和字体大小</FONT>的标记<br>  

      4.数组:

原文地址:https://www.cnblogs.com/subtract/p/3870880.html