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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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



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

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

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

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
