`

PHP开发笔记系列(二)-字符串使用

阅读更多
   
    经过了《PHP开发笔记系列(一)-PDO使用》,今天开了关于PHP开发中字符串的处理,《PHP开发笔记系列(二)-字符串使用》,形成《PHP开发笔记系列》的第二篇。

    字符串是任何开发语言都必须处理的,在PHP中字符串可以使用单引号(')或双引号(")进行定义。那单引号和双引号不同之处在哪?那就是双引号中的变量会被变量值替换,而单引号中的内容将原样输出。下面将日常程序开发中会碰到的字符串处理场景整理。


1. 以数组形式访问字符串(strlen)
file:str-lengh.php
url:http://localhost:88/str/str-lengh.php
<?php
    $word = 'Hello, Ryan!';
    echo "String($word)'s length: ".strlen($word)."<br/>";
    
    // for循环访问数组
    //for($i=0; $i<strlen($word); $i++){
    //    echo $word[$i],"<br/>";
    //}
    
    // while循环访问数组
    $i=0;
    while($i<strlen($word)){
         echo $word[$i],"<br/>";
         $i++
    }
?>

2. 去除文本中的所有HTML标记(strip_tags)
file:str-strip-tags.php
url:http://localhost:88/str/str-strip-tags.php
<?php
    // 字符串中的所有html标签都闭合
    $text = "<h1>hello world!</h1><h1>hello world!</h1><h1>hello world!</h1>";
    
    // 输出原始的字符串内容
    echo "Original Text:";
    echo $text."<br/>";
    
    // 去除所有html标签后进行输出
    echo "Destination Text(After strip_tags)"."<br/>";
    echo strip_tags($text)."<br/>";

    // 字符串中的html标签不闭合
    $text = "<h1>hello world!";
    
    // 去除所有html标签后进行输出
    echo "Original Text:";
    echo $text."<br/>";
    
    // 去除所有html标签后进行输出
    echo "Destination Text(After strip_tags)"."<br/>";
    echo strip_tags($text)."<br/>";
?>

    备注:如果$text的值是<h1>hello world!,少了</h1>,那么<h1>将不会被strip_tags函数去除,从而影响后面的格式输出,使后续的所有输出都有h1标题的样式。

3. 转义html实体(rawurlencode)
file:str-entities.php
url:http://localhost:88/str/str-entities.php
<?php
    $text = "hello & world!";
    
    echo $text."<br/>";
    
    echo rawurlencode($text)."<br/>";
?>


4. 强制文本折行显示(wordwrap)
    wordwrap函数可以按照指定的字符串折行长度,将长文本进行折行。
file:str-wordwrap.php
url:http://localhost:88/str/str-wordwrap.php
<?php
    $text = "This document covers the JavaTM 2 Platform Standard Edition 5.0 Development Kit (JDK 5.0). Its external version number is 5.0 and internal version number is 1.5.0. For information on a feature of the JDK, click on its component in the diagram below.";
    
    echo "Original text:"."<br/>";
    echo $text."<br/>";
    
    echo $text."<hr/>";
    
    echo "Destination text(after wrap):"."<br/>";
    echo wordwrap($text, 50, "<br/>")."<br/>";
?>


5. 字符串定位与替换(strpos、str_replace)
    字符串定位使用strpos函数,该函数返回一个字符串在另一个字符串出现的第一个位置,类似于JAVA中String类的indexOf()方法的作用:
file:str-strpos.php
url:http://localhost:88/str/str-strpos.php
<?php
    $text = "hello world!";
    
    echo strpos($text, "e");  
?>


    字符串替换使用str_replace函数,该函数替换部分字符串中的文本,类似于JAVA中String类的replace()方法的作用:
file:str-strreplace.php
url:http://localhost:88/str/str-strreplace.php
<?php
    $text = "This document covers the JavaTM 2 Platform Standard Edition 5.0 Development Kit (JDK 5.0). Its external version number is 5.0 and internal version number is 1.5.0. For information on a feature of the JDK, click on its component in the diagram below.";
   
    echo "Original text:"."<br/>";
    echo $text."<br/>";
    
    echo "<hr/>";
    
    echo "Destination text(replace):"."<br/>";
    echo str_replace(" ", "__", $text)."<br/>";    
?>


6. 字符串比较(substr_compare)
    字符串比较可用于比较两个字符串间的大小,类似于JAVA中String的compare方法,如果返回值>0,代表第一个字符串比第二个大,反之第二个比第一个大,若为0,表示相等。
file:str-compare.php
url:http://localhost:88/file/str-compare.php
<?php
    $main_str = 'hello world';
    $str = 'hello world, Ryan!';
    echo substr_compare($main_str, $str, 0);
?>


7. 字符串截取(substr)
    字符串截取可用于从字符串的指定位置截取指定长度的字串,用于子串值抽取很方便。
file:str-sub.php
url:http://localhost:88/file/str-sub.php
<?php
    $str = 'hello world,today is sunday!';
    $start = strpos($str, ',');
    $newStr = substr($str, $start+1);

    echo 'Original String: '.$str.'<br/>';
    echo 'Destination String: '.$newStr.'<br/>';
?>


8. 统计子串出现次数(substr_count)
    统计子串在父串中出现的次数,可以使用substr_count函数。
file:str-count.php
url:http://localhost:88/file/str-count.php
<?php
    $str = 'abcdefgacef';
    
    echo substr_count($str, 'a');
?>


9. 字符串分拆与拼装(explode、implode)
    字符串分拆可将一个字符串按照一个指定分隔符拆分成数组,类似于JAVA中String类的spilt()方法的作用。字符串组装时将字符串数组按照一个分隔符将数组中的数据进行拼装,形成一个新字符串。
file:str-explode-implode.php
url:http://localhost:88/str/str-explode-implode.php
<?php
    $text = "This document covers the JavaTM 2 Platform Standard Edition 5.0 Development Kit (JDK 5.0). Its external version number is 5.0 and internal version number is 1.5.0. For information on a feature of the JDK, click on its component in the diagram below.";
   
    echo "Original text:"."<br/>";
    echo $text."<br/>";
    
    echo "<hr/>";
    
    $sentenses = explode(". ", $text);
    echo "Destination text(explode):"."<br/>";
    foreach ($sentenses as $sentense){
        echo $sentense."<br/>";
    }
    
    echo "<hr/>";
    
    $newText= implode($sentenses, ". ");
    
    echo "Destination text(implode):"."<br/>";
    echo $newText."<br/>";    
?>


10. 去除字符串的前后空格(trim)
file:str-trim.php
url:http://localhost:88/str/str-trim.php
<?php
    $text = "   hello world!  ";
    
    echo "Original text:"."<br/>";
    echo strlen($text)."<br/>";
    
    echo "<hr/>";
    
    echo "Destination text(trim):"."<br/>";
    echo strlen(trim($text))."<br/>"; 
?>


11. 格式化输出(printf)
    格式化输出使用printf函数或sprintf函数,类似于C语言中的printf函数的作用:
file:str-printf.php
url:http://localhost:88/str/str-printf.php
<?php
    $format = 'hello, %2$s, userNo: %1$s';
    $who = 'Ryan';
    $no = '10';
    
    echo printf($format, $no, $who);    
?>


    本文地址:http://ryan-d.iteye.com/blog/1543225
分享到:
评论

相关推荐

    PHP学习笔记之字符串编码的转换和判断

    // 将字符串由 GBK 编码转换为 UTF-8 编码 但 iconv 只能解决编码预先知道的情况,如果字符串编码未知,则需要先探测其编码,这时可能会用到 mb_string 扩展库: 复制代码 代码如下:mb_detect_encoding(‘软件开发...

    PHP移动后端开发课堂笔记

    PHP移动后端开发课堂笔记 以下是文章结构目录: 一、软件开发及其准备 如何配置XAMPP 二、PHP基础 初识PHP 2.1 PHP标记与注释 2.1.1 PHP标记 2.1.2 注释 2.1.3 语句结束符 2.2 变量与常量 2.2.1 变量 2.2.2 常量 2.3...

    PHP反转字符串函数strrev()函数的用法

    呵呵,好玩吧,一真的想做一个函数百科网,只是由于我的精力有限了,只写WEB开发笔记,一天一篇文章的更新就已经够忙了,因为,我的职业也不只是写这一个博客,还有其它很多网站需要维护,天天就是写软文,发原创,真...

    Poslaju-Tracking-API:返回 Poslaju 跟踪详细信息的 JSON 格式字符串

    Poslaju跟踪API 返回 Poslaju 跟踪详细信息的 JSON 格式字符串我一直在寻找 Poslaju API,但找不到真正有效且易于使用的 API,因此,我开发了自己的 API。 可用于在您自己的项目/系统中跟踪 Poslaju 包裹笔记: 这...

    思库教育PHP零基础培训+进阶课程+PHP项目开发实战 21G PHP零基础学习视频教程.txt

    │ │ ├[思库教育]php 第12 集字符串数据类型.avi │ │ └[思库教育]php 第13集 其它数据类型介绍.avi │ ├ │ │ ├[思库教育]php 第14集 .数据类型转换.avi │ │ └[思库教育]php 第15集 流程控制语句.avi │ ...

    revolutionary-tech-notes:伊恩·布恩(Ian Bunn)的革命性技术笔记

    革命性的技术笔记 这些笔记帮助我度过了一些难关!#,我想与您分享这一点以及您的个人革命。 继续前进。 和平, 目录 [](aws / api-gateway-... 加密PHP解密节点[ 用PHP加密字符串并在Node.js中解密](javascript /

    若干源程序资料12.rar

    2012-06-11 21:40 60 access连接字符串.txt 2012-06-11 21:08 666 adc-test.c 2012-06-11 21:07 765,000 AS3游戏编程大学.pdf 2012-06-11 21:40 750,563 ATL开发指南源码.rar 2012-06-11 21:05 186,863 BIOS练习工具...

    javascript入门笔记

    2、1995年 Netscape(网景) 开发了一款语言 LiveScript,更名为 Javascript 3、1996年 Microsoft(微软) 开发了一款语言 JScript 4、1997年 网景 将Javascript 1.1 提供给了ECMA(欧洲计算机制造商联合会),ECMA 获取...

    MySQLDBA运维笔记.pdf

    1.4.1 防 SQL 注入(WEB), php.ini 配置文件里面设置 ..........................................21 1.4.2 mysql 的备份的脚本给 700 的权限,并且属主和属组为 root..........................21 1.4 关于 mysql...

    BedrockData:从Minecraft生成的数据块

    block_id_map.json 该文件包含所有块字符串ID到旧版数字ID的映射(仍在内部使用,并且第三方开发人员仍需要它们用于转换和项目)。笔记块的旧版ID&gt; 255时,其项ID为255 - legacyBlockId 。 这意味着棱柱形楼梯= -2,...

Global site tag (gtag.js) - Google Analytics