Table of Contents
PHP中一些可以替代正则表达式函数的字符串操作函数,php正则表达式
Home php教程 php手册 PHP中一些可以替代正则表达式函数的字符串操作函数,php正则表达式

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

Jun 13, 2016 am 09:20 AM
mysql php String operations

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:用另外一个字符串替换字符串的所有实例

复制代码 代码如下:


/*
 * 用另外一个字符串替换字符串的所有实例
 * 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 "
".$email;
?>

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

复制代码 代码如下:


/*
 * 获取字符串的一部分
 * strstr()函数返回字符串中从预定义的字符串的第一个出现开始的剩余部分(包括occurrence这个字符串)。其形式为:
 * string strstr(string str,string occurrence[,bool fefore_needle])
 * 可选参数before_needle会改变strstr()的行为,使函数返回字符串在第一个出先之前的部分。
 * 下面的例子是获取右键中的域名,结合ltrim()函数
 */
$url="lvchenyang@live.cn";
echo "
".ltrim(strstr($url, "@"),"@");
?>

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

复制代码 代码如下:


/*
 * substr()函数返回字符串中位于start和start+length之间的部分,其形式为:
 * string substr(string str,int start [,int length])
 * 如果没有指定的可选参数,则返回从start到str末尾的字符串
 * 如下所示
 */
$str="lvchenyang";
echo "
".substr($str, 2,4);
//output: chen
?>

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

复制代码 代码如下:


/*
 * 确定字符串出现的频率
 * substr_count()返回一个字符串在另外一个字符串中出现的次数。其形式为:
 * int substr_count(string str,string substring [,int offset [,int length]])
 * 可选参数offset和length指定字符串便宜(从便宜处开始尝试匹配字符串)和字符串长度(从便宜开始搜索的长度)
 * 下面的例子确定了每个单词在这个sentence中出现的次数
 */
$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 "
";
$sentencearray=explode(" ", $talk);
foreach ($sentencearray as $item)
{
 echo "The word $item appears(".substr_count($talk, $item).")times
";
}
?>

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

复制代码 代码如下:


/*
 * 用另外一个字符串替换一个字符串的一部分
 * substr_replace()函数将字符串中的一部分用另一个字符串替换,替换从指定的start位置开始,知道start+length位置结束。
 * 其形式为:
 * stringsubstr_replace(string str,string repalcement,int start和length的值。
 * 如下所示,替换电话号码中间4位
 */
$phonenum="15926841384";
echo "
".substr_replace($phonenum, "****", 3,4);
?>

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1253
29
C# Tutorial
1227
24
Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

The Continued Use of PHP: Reasons for Its Endurance The Continued Use of PHP: Reasons for Its Endurance Apr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Apr 18, 2025 am 08:42 AM

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

See all articles