PHP 正则(2)
PHP 正则 有两个比较重要的方法 preg_match() 和 preg_replace() ; 给一个简单的例子。要对这个进行匹配,用到的模式如下 $str = "123123,123123,123123,213123,234234" ; $model = "/^([0-9]*),(([0-9]*),))*([0-9]*)$/" ; $match = array (); $ret = preg_m
PHP 正则
有两个比较重要的方法 preg_match() 和 preg_replace(); 给一个简单的例子。要对这个进行匹配,用到的模式如下
<code><span>$str</span> = <span>"123123,123123,123123,213123,234234"</span>; <span>$model</span> = <span>"/^([0-9]*),(([0-9]*),))*([0-9]*)$/"</span>; <span>$match</span> = <span>array</span>(); <span>$ret</span> = preg_match(<span>$model</span>, <span>$str</span>, <span>$match</span>); <span>$match</span> 中就有匹配到的内容。</code>
贪婪匹配 和 非贪婪匹配 可以参考下面的例子
<code><span>$eee</span> = <span>"(12)-123 23-(233)"</span>; <span>//贪婪匹配的例子 这个模式只能匹配 一个。</span> <span>$preg</span> = <span>"/\(.*\)/"</span>; <span>$match</span> = <span>array</span>(); <span>$ans</span> = preg_match_all(<span>$preg</span>,<span>$eee</span>,<span>$match</span>); var_dump(<span>$match</span>); <span>// 结果是只能匹配一个模式。因为第一个从整体看来,是一个完整的括号,所以会匹配整个结果。</span> <span>//非贪婪匹配模式,可以把所有的括号中的内容都找出来,却别就是*后面的问号。</span> <span>$preg</span> = <span>"/\(.*?\)/"</span>; <span>$match</span> = <span>array</span>(); <span>$ans</span> = preg_match_all(<span>$preg</span>,<span>$eee</span>,<span>$match</span>); var_dump(<span>$match</span>);</code>
正则替换
preg_replace() 可以对 $str 中的内容进行替换。还是上面的例子,这次我们把$eee中国年的括号里面的数字替换成字符串haha.可以设计出如下的方法。注意 preg_replace 的用法。$pattern 和 $replace 都是array,下面是具体的匹配模式。
<code>按照正则表达式的规则对匹配的内容进行替换。 <span>$eee</span> = <span>"(12)-123 23-(233)"</span>; <span>$preg</span> = <span>array</span>(<span>"/\(.*?\)/"</span>); <span>$replace</span> = <span>array</span>(<span>"haha"</span>); <span>$ans</span> = preg_replace(<span>$preg</span>,<span>$replace</span>,<span>$eee</span>); var_dump(<span>$ans</span>);</code>
上面是比较基本的用法,下面这个稍微复杂一些。
<code><span>$str</span> = preg_replace(<span>"/(<a.>)(.*?)()/"</a.></span>,<span>'\1<span class="link">\2</span>\3'</span>, <span>$str</span>); <span>// 通过学习上面的表达是,我们可以将我们前面的例子 转化成这样。</span> <span>$eee</span> = <span>"whie2ch(12)-123 23-function(233)"</span>; <span>$preg</span> =<span>"/(\w*)\(.*?\)/"</span>; <span>$replace</span> =<span>"\1word\2bb"</span>; <span>// 将变量换成word,将括号及括号里面的内容换成bb \1 \2 代表第几个括号里面匹配的内容。</span> <span>$ans</span> = preg_replace(<span>$preg</span>,<span>$replace</span>,<span>$eee</span>); var_dump(<span>$ans</span>);</code>
PHP 修正符
<code><span>$eee</span> =<span>"whie2ch(1 \n2)-123 n 23-function nhehe(23 \n3) sfsdf(24 \n5)"</span>; <span>$preg</span> =<span>"/\(.*?\)/"</span>; <span>//$replace ="\1word\2bb";</span> <span>$match</span> = <span>array</span>(); <span>$ans</span> =preg_match_all(<span>$preg</span>,<span>$eee</span>,<span>$match</span>); var_dump(<span>$match</span>); <span>// 这样,发现是匹配不到括号里面的内容的。因为.不包括换行。</span> <span>// 使用方法,把上面的 $preg 改成下面这样。</span> <span>$preg</span> =<span>"/\(.*?\)/s"</span>; 重新运行程序,发现所有的内容都匹配出来了。</code>
PHP 的几个修正符如下
修正符 | 含义 |
---|---|
s | 让.包括换行,可以实现跨行匹配 |
U | 让?默认匹配成贪婪状态 |
i | 忽略大小写 |
u | 模式串被当做UTF8 |
m | 当 设定了此修正符,行起始(^)和行结束($)除了匹配整个字符串开头和结束外,还分别匹配其中的换行符(\n)的之后和之前 |
PHP修正符 的使用方式入上面的程序所示。将/your_regrex/X
,其中X代表你的修正符,根据需要选择。

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 and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
