您的位置:首页 > 编程语言 > PHP开发

PHP中一些可以替代正则表达式函数的字符串操作函数

2014-11-17 00:00 766 查看
0x01:根据预定义的字符对字符串进行词法分析

<?php

/*

 * 在处理大量信息时,正则表达式函数会使速度大幅减慢。应当在需要使用正则表达式解析比较复杂的字符串时才能使用这些函数。如果要解析简单的表达式,还可以采用很多可以显著加快处理过程的预定义函数。

 */
/*

 * 根据预定义的字符对字符串进行词法分析

 * strtok()函数根据预定义的字符列表来解析字符串。其形式为:

 * string strtok(string str,string tokens)

 * strtok()函数,必须连续调用这个函数,才能完全的对一个字符串进行词法分析;每次调用该函数只是对字符串的下一部分做词法分析。但是,str参数只需要指定一次,因为函数会跟踪str中的位置,知道完全对str完成了词法分析,或者指定了心得str参数。

 * 如下面的例子所示:

 */

$info="lv chen yang|Hello:world&757104454@qq.com";

//定义界定符,包括(|)(:)( )(&)

$tokens="|:& ";

$tokened=strtok($info, $tokens);

while ($tokened)

{

 echo "Element:$tokened<br/>";

 //连续调用strtok()函数,完成对整个字符串的词法分析

 $tokened=strtok($tokens);

}

?>

0x02:根据预定义的定界符分解字符串

<?php 

/*

 * 根据预定义的定界符分解字符串:explode()函数

 * 次函数将字符串str分成子串数组,其形式为:

 * array explode(string separator,string str [, int limit])

 * 原字符串被根据separator指定的字符串分割为不同的元素。元素的数量可以通过可选的参数limit来限制。可以结合explode()/sizeof()和strip_tags()来确定给定文本块中单词的总数

 * 如下所示:

 */

$summary="

   In the latest installment of the ongoing Developer.com PHP series.

   I discuss the many improvements and addtions to

   <a href=\"http:www.php.com\">PHP</a> object-oriented architecture. 

   ";

echo "<br/>";

$words=explode(' ', strip_tags($summary));

echo "This sentence's lenght is:".sizeof($words);

/*

 * explode()函数始终比preg_split、spilt()和spliti()快得多。因此,在不需要使用正则表达式时,一定要使用这个函数。

 */

?>

0x03:将数组转换成字符串

<?php 

/*

 * 将数组转换成字符串

 * explode()函数可以根据界定字符将字符串转换为相应的数组,但是可以通过implode()函数将数组转换为规定的界定字符为界限的字符串

 * 其形式为:

 * string implode(string delimiter,array pieces)

 * 如下所示:

 */

$citys=array("Chengdu","Chongqing","Beijing","Shanghai","Guangzhou");

$citystring=implode("|", $citys);

echo $citystring;

?>

0x04:解析复杂的字符串

<?php 

/*

 * 解析复杂的字符串

 * strpos()函数在字符串中以区分大小写的方式找到substr第一次出现的位置,其形式为

 * int strpos(string str,string substr [,int offset])

 * 可选参数offset指定开始搜索的位置。如果substr不在str中,则strpos()返回False。可选参数确定strpos()从哪里开始搜索。

 * 以下例子将确定第一次访问index.html的时间戳:

 */

$substr="index.html";

$log=<<<logfile

192.168.1.1:/www/htdocs/index.html:[2013/06/26:13:25:10]

192.168.1.2:/www/htdocs/index.html:[2013/06/26:13:27:16]

192.168.1.3:/www/htdocs/index.html:[2013/06/26:13:28:45]

logfile;

echo "<br/>";

//$substr在log中首次出现的位置是什么

$pos=strpos($log, $substr);

//查找行结束的数值位置

$pos1=strpos($log,"\n",$pos);

//计算时间戳的开始

$pos=$pos+strlen($substr)+1;

//检索时间戳

$timestamp=substr($log, $pos,$pos1-$pos);

echo "The file index.html was first accessed on: $timestamp<br/>";

/*

 * 函数stripos()和函数strpos()函数用法相同,唯一的区别是stripos()不区分大小写。

 */

?>

0x05:找到字符串最后一次出现的位置

<?php 

/*

 * 找到字符串中最后一次出现的位置

 * strrpos()函数搜索字符串的最后出现的位置,返回它的位置(数值序号)其形式为:

 * int strrpos(string str,char substr [,offset])

 * 可选参数offset确定strrpos()函数的开始搜索位置。加入希望缩短冗长的新闻总结,

 * 截取总结中的某些部分,并用省略号代替所截去的部分。然而,并非简单的将总结明确的剪为所需的长度,

 * 你可能希望以一种对用户友好的方式进行剪切,截取到与阶段长度最接近的单词末尾。

 * 如下例子所示

 */

$limit=100;

$summary="In the latest installment of the ongoing Developer.com PHP series.

   I discuss the many improvements and addtions to

   <a href=\"http:www.php.com\">PHP</a> object-oriented architecture. ";

if(strlen($summary)>$limit)

 $summary=substr($summary, 0,strrpos(substr($summary, 0,$limit)," "))."...";

echo  $summary;

?>

0x06:用另外一个字符串替换字符串的所有实例

<?php 

/*

 * 用另外一个字符串替换字符串的所有实例

 * str_replace()函数以区分大小写的方式用另外一个字符串奇幻某个字符串的所有实例。其形式为:

 * mixed str_replace(string occurrence, mixed replacement, mixed str [,int count])

 * 如果str中没有找到occurrence,则str保持不变,如果定义了可选参数count,则只替换str中count个currence。

 * 此函数很适合对自动获取电子邮箱地址的程序隐藏电子右键地址,如下所示:

 */

$email="lvchenyang@live.cn";

$email=str_replace("@", "(at)", $email);

echo "<br/>".$email;

?>

0x07:获取字符串的一部分

<?php

/*

 * 获取字符串的一部分

 * strstr()函数返回字符串中从预定义的字符串的第一个出现开始的剩余部分(包括occurrence这个字符串)。其形式为:

 * string strstr(string str,string occurrence[,bool fefore_needle])

 * 可选参数before_needle会改变strstr()的行为,使函数返回字符串在第一个出先之前的部分。

 * 下面的例子是获取右键中的域名,结合ltrim()函数

 */

$url="lvchenyang@live.cn";

echo "<br/>".ltrim(strstr($url, "@"),"@");

?>

0x08:根据预定义的便宜返回字符串的一部分

<?php 

/*

 * substr()函数返回字符串中位于start和start+length之间的部分,其形式为:

 * string substr(string str,int start [,int length])

 * 如果没有指定的可选参数,则返回从start到str末尾的字符串

 * 如下所示

 */

$str="lvchenyang";

echo "<br/>".substr($str, 2,4);

//output: chen

?>

0x09:确定字符串出现的频率

<?php

/*

 * 确定字符串出现的频率

 * substr_count()返回一个字符串在另外一个字符串中出现的次数。其形式为:

 * int substr_count(string str,string substring [,int offset [,int length]])

 * 可选参数offset和length指定字符串便宜(从便宜处开始尝试匹配字符串)和字符串长度(从便宜开始搜索的长度)

 * 下面的例子确定了每个单词在这个sentence中出现的次数

 */

$talk=<<<talk

I am acertain that we could dominate mindshare in this space with

our new product, extablishing a true synergy beteen the marketing

and product development teams. We'll own this space in thress months.

talk;

echo "<br/>";

$sentencearray=explode(" ", $talk);

foreach ($sentencearray as $item)

{

 echo "The word <strong>$item</strong> appears(".substr_count($talk, $item).")times<br/>";

}

?>

0x10:用另一个字符串替换一个字符串的一部分

<?php 

/*

 * 用另外一个字符串替换一个字符串的一部分

 * substr_replace()函数将字符串中的一部分用另一个字符串替换,替换从指定的start位置开始,知道start+length位置结束。

 * 其形式为:

 * stringsubstr_replace(string str,string repalcement,int start和length的值。

 * 如下所示,替换电话号码中间4位

 */

$phonenum="15926841384";

echo "<br/>".substr_replace($phonenum, "****", 3,4);

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐