php中字符串和正则表达式详解,php正则表达式详解
php中字符串和正则表达式详解,php正则表达式详解
一、字符串类型的特点
1、PHP是弱类型语言,其他数据类型一般都可以直接应用于字符串函数操作。
1 |
|
2、字符串可以作为“数组”,是字符的集合。
1 |
|
但是字符串不是真的数组,不能使用数组的函数.如count($str)不会返回字符串长度。PHP引擎无法区分字符和数组,产生二义性。自PHP4起,已经用花括号替代方括号。
1 |
|
3、双引号变量解析
在PHP中,当用双引号或者定界符定义字符串时,其中的变量会被解析。
1 |
|
二、字符串输出函数
三、常用的字符串格式函数
PS:PHP的字符串处理函数大部分不对源字符串做修改,而是返回新的字符串
四、正则表达式
正则表达式描述了一种字符串匹配的模式,通过这个模式在特定的函数中对字符串进行匹配、查找、替换和分隔等操作,由原子、元字符和模式修正符三部分组成的文字模式。
在PHP中,有两套正则的处理函数库:PCRE和POSIX。前者以preg_前缀命名,与Perl兼容;后者以ereg_前缀命名。二者功能相似,但PCRE的效率略高。
与Perl语言兼容的正则表达式处理函数:
1、语法
1.1 定界符:在与Perl兼容的正则函数中使用模式时,必须给模式加上定界符。除了字母、数字和反斜线(\)之外的任何字符都可以作为定界符号
1 |
|
1.2 原子:原子包含了普通字符,如字母、数字;非打印字符,如空格、回车;特殊字符和元字符,如引号、*、+等,必须用”\”进行转义;自定义原子表,如[apj]、[a-z];通用字符类型,如\d、\D。
1 |
|
1.3 元字符:用于构建正则表达式的具有特殊含义的字符。Perl可以使用各种元字符来搜索匹配,如*、+、?.常见的元字符如下
1.4 模式修正符:在正则的定界符之外使用,扩展正则在匹配、替换等方面的功能。
2.与Perl兼容的正则表达式函数
2.1 preg_match(string pattern,string subject[,array matches]):用于对字符串的查找和匹配。参数说明:
pattern是正则,subject是需要处理的字符串,可选的matches用于保存于pattern的各个子模式的匹配结果,matches[0]保存了与pattern匹配的整体内容,matches[1]保存了pattern中第一个小括号中匹配的内容,以此类推。
1 |
|
结果
preg_match_all()与preg_match()函数类似,不同的是前者会一直匹配到字符串末尾,后者在第一次匹配后就停止匹配。
2.2 preg_grep(string pattern,array iput):匹配数组中的元素,返回与正则匹配的数组单元。参数说明:
pattern是正则,input是需要匹配的数组。
1 |
|
2.3 preg_replace(mixed pattern,mixed replacement,mixed subject[,int limit]):字符串替换。说明:
该函数会在subject中搜索与pattern的匹配项,并用replacement替换。limit用于限制匹配的次数,即替换的次数。
1 |
|
2.4 preg_split(string pattern,string subject[,int limit[,int flags]]):对字符串进行分割。说明:
函数返回一个数组。数组元素包含subject中与pattern匹配作为边界所分割的字符串,limit含义见2.3,flags含义请参考文档。
1 |
|
PHP 正则表达式+字符串提取问题
$str='{"a":1234567890,"b":"u","birthday":"2000-01-01","gender":"1","location":"123456","login_ip":"123.123.123.123","login_time":1234567890,"id":"1234567","sign":"0bcbdea54d1f2c3c75b058eb5d2ae124"}';
$user_id="";
if (preg_match_all('|"id":"(\S+?)"|', $str, $reg))
{
$user_id=$reg[1][0];
}
echo "UserID is : ".$user_id;
?>
输出结果:
UserID is : 1234567
PHP中怎用正则表达式给一段字符串内加内容?
可以用正则表达式,也可以直接用php,感觉还是php比较方便一点:
$str = str_replace("","

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.
