码迷,mamicode.com
首页 > Web开发 > 详细

PHP字符串操作大集合

时间:2017-07-23 22:49:02      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:wal   格式   定义   返回值   money   1.5   位置   转义   参考   

字符串的处理非常重要。文本字符串中的空格或者其他没有意义的符号。例如,在一个电子商务应用中,当用户填写订单的内容时(如联系地址),可能输入一些空格、句号等PHP4及以上版本提供了4个去除字符串str首尾处空格或其他特殊符号。

技术分享    string ltrim(string str [, string charlist]):去除字符串str尾的空格或其他特殊符号。

技术分享    string chop(string str [, string charlist]):功能同rtrim()。

以上函数的第1个参数str为待操作的字符:空格("")、制表符(\t)、换行符(\n)、回车符(\r)、空值(\0)。还可以使用“..”通过第2个参数指定需要去除一个范围内的字符,即a、b、c、d。

下面以trim()为例说明上述函数的使用。

1   <!--去除字符:trim.2   <?  //前后都包含一个空格

4       echo $init_str."#<br>";

5       $trimmed_str =trim($init_str);       //"山东省济南市经十路8号1."

6       echo $trimmed_str."#<br>";

7       $trimmed_str = trim($init_str,‘,.‘);//"山东省济南市经十路8号1"。注意,第2个参数包括3个

9       $trimmed_str = trim($init_str,‘,.0..9‘);   //"山东省济南市经十路8号"。0..9说明要去掉所有的数字

11  ?>

 

第3行定义了一个字符,即去掉了首尾处的空格、逗号和句号。

第9行trim()中第2个参数中的“0..9”说明将要去掉位于0和9的ASC码范围内的所有字符‘\’,手工构造起来相当麻烦。为了解决类似问题,字符串中加入或去除转义字符串,第2个参数charlist说明需要在原始串的哪些字符‘\’。

技术分享    string stripcslashes(string str):去掉1   <!--加入转义php-->

2   <?

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">6    echo$new_str."#<br>";

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">7    $init_str2= stripcslashes($new_str);

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">8    echo$init_str2."#<br>";

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">9   ?>

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select=""> 

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">代码在第5行在$init_str中的‘’’前加上了‘\’,又在第9行将其去掉。

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">3.1.3 生成HTML元素

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">HTML元素的书写非常麻烦,下面简单列出一些常用技术分享    双引号‘"’:‘&quot;‘

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">技术分享    单引号‘‘’:‘&#039;‘

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">技术分享    ‘<‘ :‘&lt;‘

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">技术分享    ‘>‘ :‘&gt;‘

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">此处,称‘&‘等为HTML元素,‘&amp;‘等为其显示字符串。

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">技术分享    string htmlentities(string str [, int quote_style [, stringcharset]]):把所有的HTML元素转换为显示字符串转化为HTML元素。

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">上面函数中,参数str表示原始字符集。字符集参考表3.1。

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">表3.1                                              字符

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select=""> 

 

字 符 集

 

说  明

 

ISO-8859-1

 

西欧ISO-8859-15

 

西欧UTF-8

 

兼容ASCII的宽字节cp1252

 

西欧BIG5

 

繁体中文,用于中国台湾省

 

GB2312

 

简体中文,用于中国大陆

 

BIG5-HKSCS

 

繁体中文扩展,用于中国香港

 

Shift_JIS

 

日文

 

EUCJP

 

日文

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select=""> 

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select=""> 

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">下面的示例中,首先使用htmlentities()函数得到一个HTML语句的显示字符串转回HTML元素。运行结果如图3.1所示。

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select=""> 

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">1   <!--生成HTML元素:htmlspecialchars.2   <?

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">4    $a =htmlentities($orig,ENT_COMPAT,"GB2312");

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">5    $b =html_entity_decode($a);

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">6    echo $a; //I‘ll &quot;walk&quot; the&lt;b&gt;dog&lt;/b&gt;now

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">7    echo $b; //I‘ll "walk" the<b>dog</b>now

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">8   ?>

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">技术分享

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">图3.1

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select=""> 

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">技术分享注意

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select=""> 

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">函数html_entity_decode()只支持 

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">除上面所提到的3个函数之外,用于HTML元素操作的函数还包括nl2br()、get_html_translation_table()等,功能与上述函数类似,本书不再一一详述。

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">3.1.4 分解字符串是指把一个字符串“2005-01-0112:59:59”可以利用符号“-”、空格和“:”分解为年月日时分秒具体的值。其中,参数pattern指定了作为分解标识的符号;str为待操作的原始串;第3个可选参数limit为返回子串个数的最大值,缺省时为全部返回。函数的返回值为数组,将在3.2节对其进行介绍。此处,可以暂时把函数返回值理解为多个子串。

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">下面的示例可以把1   <!--分解php-->

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">2   <?3    $date ="2005-01-01 12:59:59";

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">4    list($year,$month,$day,$hour,$minite,$second) = split (‘[-:]‘,$date);

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">5   echo"{$year}年{$month}月{$day}日{$hour}时{$minite}分{$second}秒<br>\n";

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">6   ?>

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select=""> 

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">上例将输出“2005年01月01日12时59分59秒”。第4行使用split函数把时间分解,分解的标识符包括“-”、空格和“:”,在第5行将其输出。

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">除split之外,功能相似的函数还包括preg_split(),explode(),implode(),chunk_split()和wordwrap()等。

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">3.1.5 格式化字符串用于按一定的格式输出含有许多变量的文本,是最常用的一种操作。参数format是转换后的格式,各个变量都以“%”后的1  <!--格式化php-->

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">2   <?

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">3   $name="张三";

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">4    $money1 =68.75;

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">5    $money2 =54.35;

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">6    $money =$money1 + $money2;

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">7    // 此时变数$money 值为 "123.1";

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">8    $formatted= sprintf ("%s有¥%01.2f。",$name, $money);

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">9    echo$formatted;   //张三有¥123.10。

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">10  ?>

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select=""> 

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">第6行通过算术运算,得到$money的值为123.1;而在第8行通过sprintf中的%01.2定义其格式为显示小数点后两位。

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">除sprintf()之外,常用于格式化数据的函数还有printf()、sprintf()、sscanf()、fscanf()、vsprintf()和number_format()等。

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">3.1.6 获取子串是指从一个串中PHP提供了两个函数来获取子串,第1个参数str是待操作的串,第2个参数start表明子串在总串中的起始位置,第3个可选参数指定所获取的基础上进行替换,即将获取串“2005-01-0112:59:59”的时间信息,然后使用substr_replace()函数将年份信息改为“2006”:

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select=""> 

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">1   <!--php-->

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">2   <?

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">3       $date = "2005-01-01 12:59:59";

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">4       $time=substr($date,11,8); //子串"12:59:59"的起始位置为11,长度为8

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">5       echo "time:$time<br>";

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">6       $new_date=substr_replace($date,"2006",0,4);

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">7       echo "new date:$new_date";

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">8   ?>

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">3.1.7 定位字符是指寻找某个字符。下面这个示例,对一个电子邮件地址进行处理,首先使用strpos()寻找获取子串函数strstr()1   <!--php-->

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">2   <?

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">3       $email = "zhangsan@4       $i=strpos($email,‘@‘);

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">5       $name=substr($email,0,$i);

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">6       echo $name;

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">7   ?>

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select=""> 

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">示例第4行使用strpos()字符’@’的位置,然后在第5行使用substr()得到用户名子串信息。

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">3.1.8 求串长度

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">求串长度也是常用的操作,所使用的函数为strlen():int strlen( string str)。

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">这个函数很简单,返回php.net。

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select=""> 

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">1   <!--php-->

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">2   <?

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">3       $email = "zhangsan@4       $i=strpos($email,‘@‘);

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">5       $name=substr($email,0,$i);

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">6       $email=substr_replace($email,"lisi",0,strlen($name));

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">7       echo $email;

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">8   ?>

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select=""> 

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">3.1.9 字符转化为ASCII编码在实际应用中有时是很有用的,例如,获取函数返回ASCII码串时,就需要把其转化为PHP提供的转换ASCII码和字符串。

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">技术分享    int ord(string string):把1   <!--ASCII转换:chr.2   <?

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">3       $letter = chr(65);      //A

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">4       $ascii=ord(‘A‘);        //65

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">5       echo $letter;

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">6       echo   $ascii;

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">7   ?>

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select=""> 

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">3.1.10 比较字符串的比较规则是按照字典排序方法,排在前面的小于后面的。如同在一本英语词典中,后面的词条大于前面的词条。字符串比较的函数如下。

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">技术分享    int strncmp(string str1, string str2[, intlen]):函数的前两个参数为待比较的两个字符。如果str1>str2,函数返回正数;str1=str2时返回0;str1<str2时返回负数。

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">1   <!--ASCII转换:chr.2   <?

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">3       $str1="China";

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">4       $str2="Beijing";

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">5       $i=strcmp($str1,$str2);

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">6       echo$i;       //1

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">7   ?>

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select=""> 

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">除strcmp()之外,具有字符串是否在不区分大小写时相等,仅仅使用上一小节的strcmp()函数就不行了,这时可将两个PHP实现技术分享    string strtoupper(string string):将str转换为大写形式。

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">技术分享    string ucfirst(string str):将str的第一个参考下例。

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select=""> 

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">1   <!--大小写转换:Upper_Lower.2   <?

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">3       $str1="shandong province";

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">4       $str2="China";   

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">5       $str1=ucwords($str1);

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">6       echo$str1;         //ShangdongProvince   

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">7       $str1=strtoupper($str1);

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">8       echo $str1;        //SHANGDONGPROVINCE   

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">9       $str2=strtolower($str2);

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">10      echo$str2;         //china

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">11   ?>

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select=""> 

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">3.1.12 小结

4    echo$init_str."#
";

5    $new_str =addcslashes($init_str,"‘ where="" books="" from="" *="" select="">PHP中应用最为广泛的数据类型,其操作也种类繁多。字符串操作函数,熟练地使用这些函数,是使用PHP函数手册。

PHP字符串操作大集合

标签:wal   格式   定义   返回值   money   1.5   位置   转义   参考   

原文地址:http://www.cnblogs.com/xiongxionglove/p/7225932.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!