Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-06-06 20:17:15
Original
1108 people have browsed it

这篇文章主要介绍了PHP中一些可以替代正则表达式函数的字符串操作函数,本文总结的是一些比较特别的字符串操作函数,需要的朋友可以参考下

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

复制代码 代码如下:


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

/*
 * 根据预定义的字符对字符串进行词法分析
 * 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
";
 //连续调用strtok()函数,完成对整个字符串的词法分析
 $tokened=strtok($tokens);
}
?>

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

复制代码 代码如下:


/*
 * 根据预定义的定界符分解字符串: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
   PHP object-oriented architecture.
   ";
echo "
";
$words=explode(' ', strip_tags($summary));
echo "This sentence's lenght is:".sizeof($words);
/*
 * explode()函数始终比preg_split、spilt()和spliti()快得多。因此,在不需要使用正则表达式时,一定要使用这个函数。
 */
?>

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

复制代码 代码如下:


/*
 * 将数组转换成字符串
 * explode()函数可以根据界定字符将字符串转换为相应的数组,但是可以通过implode()函数将数组转换为规定的界定字符为界限的字符串
 * 其形式为:
 * string implode(string delimiter,array pieces)
 * 如下所示:
 */
$citys=array("Chengdu","Chongqing","Beijing","Shanghai","Guangzhou");
$citystring=implode("|", $citys);
echo $citystring;
?>

0x04:解析复杂的字符串

复制代码 代码如下:


/*
 * 解析复杂的字符串
 * strpos()函数在字符串中以区分大小写的方式找到substr第一次出现的位置,其形式为
 * int strpos(string str,string substr [,int offset])
 * 可选参数offset指定开始搜索的位置。如果substr不在str中,则strpos()返回False。可选参数确定strpos()从哪里开始搜索。
 * 以下例子将确定第一次访问index.html的时间戳:
 */
$substr="index.html";
$log= 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 "
";
//$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
";
/*
 * 函数stripos()和函数strpos()函数用法相同,唯一的区别是stripos()不区分大小写。
 */
?>

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

复制代码 代码如下:


/*
 * 找到字符串中最后一次出现的位置
 * 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
   PHP object-oriented architecture. ";
if(strlen($summary)>$limit)
 $summary=substr($summary, 0,strrpos(substr($summary, 0,$limit)," "))."...";
echo  $summary;
?>

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

复制代码 代码如下:

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!