首页 > 其他 > 详细

字符串处理

时间:2015-10-15 14:14:52      阅读:155      评论:0      收藏:0      [点我收藏+]
一、输出字符串
    1.echo
      void echo ( string arg1 [, string ...] ) 是一个语法 ,不是函数
      echo 没有返回值;
      echo 可以输出多个值,使用逗号分隔;
       
      $val = "world";
      echo "hello", $val;
 
    2.print
      int print ( string arg )  实际上不是一个函数(它是一个语言结构)
 
       print ("hello $val");
       print "hello world";
 
    3.printf() 
      功能:格式化输出字符串;
      int printf ( string format [, mixed args [, mixed ...]] )
          
        %b二进制输出         //brianry
        %d整数输出           //data
        %f浮点数输出         //float
        %s字符串输出         //string
 
        $str = "123 test";
        printf("整数:%d", $str);
        printf("浮点数:%.2f", $str);
        printf("字符串:%s", $str);
 
    4.sprintf()
 
      功能与printf相同,但不会直接输出结果;
 
      string sprintf ( string format [, mixed args [, mixed ...]] )
 
      $str = "123 test";
      $val = sprintf("%d", $str);
      echo $val;
 
 
  二、查找与替换
 
      1.strpos()
        int strpos ( string haystack, mixed needle [, int offset] )
 
        strpos()函数在 haystack 中以区分大小写的方式找到 needle 第一次出现的位置;
        如果没有找到则返回FALSE;可选参数offset 指定开始查找的位置;
 
 
        echo strpos("Hello world!","wo");
 
 
      2.stripos()
        stripos()与strpos()功能相同,只是查找时不区别大小写;
         
 
 
      3.str_replace()
        mixed str_replace ( mixed search, mixed replace, mixed subject [, int &count] )
 
        str_replace()函数在subject中以区分大小写的方式搜索 search ,用replace替换找到的所有内容;
        如果没有找到search,则subject保持不变;
        如果定义了可选参数 count 则只替换subject中count个search
 
        $str = "test@163.com";
        $email = str_replace("@", "(at)", $str);
        echo $email;
 
 
      4.str_ireplace()
        str_ireplace()与str_replace()功能相同,只是不区分大小写;
 
 
  三、截取字符串
 
      1.substr()
      string substr ( string string, int start [, int length] )
 
      从start位置取出length长度的字符,字符串位置开始值为零;如果没有指定length,那么默认一直到字符串末尾;
 
 
      echo substr("Hello world", 6);
      echo substr("hello world", 6, 5);
 
 
      2.strstr()
 
        string strstr ( string haystack, string needle )
 
        strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。该函数返回字符串的其余部分(从匹配点)。
        如果未找到所搜索的字符串,则返回false
 
        echo strstr("Hello world!","world");
 
 
        3.stristr()
 
        stristr()与strstr()功能相同,只是不区分大小写;
 
        echo strstr("Hello world!","WORLD");
 
 
 
 
  四、删除字符串
      1.ltrim()
 
        string ltrim ( string str [, string charlist] )
        ltrim 函数删除字符串左侧空格或其他预定义字符;
 
        如果未设置charlist参数,则删除以下字符:
        "\0"  NULL
        "\t"     制表符
        "\n"     换行
        "\x0B"   垂直制表符
        "\r"     回车
        " "      空格
 
        $str = "       Hello World!";
        echo ltrim($str);
 
 
      2.rtrim()
 
        string rtrim ( string str [, string charlist] )
        rtrim 函数删除字符串右侧空格或其他预定义字符;
 
 
      3.trim()
 
        trim 函数删除字符串两侧空格或其他预定义字符;
 
 
  五、其它字符串处理函数
 
      1.strlen() 获取字符串长度
 
        $passwd = "123456";
          if(strlen($passwd) < 8){
            echo "密码不能少于8位";
          }
 
      2.strtolower() 将字符串转换为小写字母
 
          $url = "HTTP://WWW.WENGDO.COM/ ";
          echo strtolower($url);
 
 
      3.strtoupper() 将字符串转换为大写字母
          $str = "中文 hello world";
          echo strtoupper($str);
 
      4.strrev() 反转字符串
          $str = "hello world";
          echo strrev($str);
 
      5. nl2br() 将字符串中换行 (\n) 转换成 HTML 换行标签 (<br>)
          $str = "hello
          world";
          echo nl2br($str);
 
 
      6.strip_tags() 删除字符串中HTML XML PHP 标签
        string strip_tags ( string str [, string allowable_tags] )
        可选参数 allowable_tags 指定要保留的标签;
 
        $str = "test <a href="http://www.163.com">163</a>";
        echo strip_tags($str);
 
 
    7. htmlspecialchars() 函数把一些预定义的字符转换为 HTML 实体
 
        预定义的字符是:
        & (和号)   成为  &
        " (双引号) 成为  "
        ‘ (单引号) 成为  ‘
        < (小于)   成为  <
        > (大于)   成为  >
 
        $str = "<p> 这是一个段落 </p>";
        echo htmlspecialchars($str);
 
 
    练习:
 
      1.以下代码运行的结果?
        $a = "PHPlinux";
        $b = "PHPLinux";
        $c = strstr($a, "L");
        $d = stristr($b, "l");
        echo $c."is".$d;
        A. PHP is Linux
        B. is Linux
        C. PHP is inux
        D. PHP is
 
 
      2.以下代码运行的结果为?
 
        $first = "This course is very easy!";
        $second = explode(" ", $first);
        $first = implode(",", $second);
        echo $first;
 
        A. This,course,is,very,easy!
        B. This course is very easy!
        C. This course is very easy!,
        D. 提示错误
 
 
      3.下列哪个函数是将字符串前后颠倒?
 
        A. strrev();
        B. strpos();
        C. strstr();
        D. ucfirst();
 
      4.以下程序程序运行结果为: (选择题)
        Array([0]=>@test [1]=>com [2]=>cn)
        横线处应该使用的函数为?
        $email = "abc@test.com.cn";
        $str = _______($email, ‘@’);
        $info = _______(‘.’, $str);
        ______($info);
 
        A. strchr , split, var_dump
        B. strstr, explode, print_r
        C. strstr, explode, echo
        D. strchr, split, var_dump
 
      5. 在str_replace(1, 2, 3)函数中1 2 3 正确的顺序为:
        A. 取代字符串, 被取代字符串, 来源字符串
        B. 被取代字符串, 取代字符串, 来源字符串
        C. 来源字符串, 取代字符串, 被取代字符串
        D. 来源字符串, 被取代字符串, 取代字符串

字符串处理

原文:http://www.cnblogs.com/jacko/p/4882058.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!