PHP中一些可以替代正则表达式函数的字符串操作函数
这篇文章主要介绍了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:用另外一个字符串替换字符串的所有实例
复制代码 代码如下:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
