什么是正则表达式?
正则表达式(也称为“正则表达式”)是文本的搜索模式。正则表达式引擎可以处理这些模式以查找子字符串、验证字符串是否与模式匹配或搜索和替换文本。
正则表达式非常强大,让您可以毫不费力地对字符串执行搜索和替换操作。它们被广泛用于不同语言的模式匹配和验证。今天,我们将在 php 的上下文中讨论正则表达式。
在这篇文章中,我们将介绍 PHP 中处理正则表达式的不同函数。具体来说,我们将preg_*结合现实世界的例子来探索族函数。
PHP中的正则表达式函数
在本节中,我们将通过实际示例介绍 PHP 中的各种正则表达式函数。
preg_match功能_
该preg_match函数对字符串执行正则表达式匹配。让我们看一下语法:
preg_match ( string $pattern , string $subject , array &$matches = null , int $flags = 0 , int $offset = 0 );
第一个参数是您要搜索的模式。您应该以正则表达式格式提供它。第二个参数是执行正则表达式匹配的主题字符串。接下来,第三个参数是可选的,但如果提供,它将填充搜索结果。在大多数情况下,您将需要这三个参数。
第四个参数是PREG_OFFSET_CAPTURE和PREG_UNMATCHED_AS_NULL标志的组合,您可以使用它来获取有关搜索结果的一些额外信息。
最后,第五个参数是偏移量,它允许您指定搜索开始的位置。因此,如果您不想从字符串的开头开始搜索,这就是您应该使用的参数。
这就是preg_match函数的语法。让我们看几个真实世界的例子。
示例 1:简单字符串匹配
<?php preg_match('/tutsplus/i', 'The www.weixiaolive.com/en is one of my favorite websites among the group of all tutsplus websites!', $matches); /** $matches value: Array ( [0] => tutsplus ) **/
在第一个示例中,我们正在检查源文本是否包含特定单词。由于该tutsplus字符串存在于源文本中,因此该$matches变量将填充匹配的字符串。
示例 2:简单模式匹配
<?php preg_match('/code.(tutsplus).com/i', 'The www.weixiaolive.com/en is one of my favorite websites among the group of all tutsplus websites!', $matches); /** $matches value: Array ( [0] => www.weixiaolive.com/en [1] => tutsplus ) **/
在这个例子中,我们使用了模式匹配。因为它是模式匹配,所以$matches[0]元素包含匹配完整模式$matches[1]的文本,元素包含匹配第一个括号模式的文本。
示例 3:查找图像路径
<?php preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', '<img src="https://example.com/image1.jpg" /> <img src="http://example.com/image2.jpg" />', $matches); /** $matches value: Array ( [0] => <img src="http://example.com/image1.jpg [1] => http://example.com/image1.jpg ) **/
接下来,我们试图在第一个标签中找到src属性的值。<img>重要的是要注意它只返回第一个匹配项,即使有多个匹配项。
示例 4:查找特定的 html 标签
<?php preg_match('#<div class="tutsplus">(. *?)</div>#s', '<div>Some text.</div> <div class="tutsplus">Special text.</div><div>Some more text.</div>', $matches); /** $matches value: Array ( [0] => <div class="tutsplus">Special text.</div> [1] => Special text. ) **/
最后,这是一个示例,它演示了如何从特定的 HTML 标记中提取文本。
preg_match_all功能_
该preg_match_all函数与该函数非常相似,preg_match不同之处在于它执行全局正则表达式匹配。因此,它返回与模式匹配的所有值。
此外,函数的语法与preg_match_all函数的语法相同preg_match。
让我们快速通过几个例子来看看它是如何工作的。事实上,我们将使用preg_match_all函数而不是函数来修改我们在上一节中讨论过的示例preg_match。这样,它应该可以帮助您了解这两个变体之间的区别。
示例 1:简单字符串匹配
<?php preg_match_all('/tutsplus/i', 'The www.weixiaolive.com/en is one of my favorite websites among the group of all tutsplus websites!', $matches); /** $matches value: Array ( [0] => Array ( [0] => tutsplus [1] => tutsplus ) ) **/
示例 2:简单模式匹配
<?php preg_match_all('/code.(tutsplus).com/i', 'The www.weixiaolive.com/en is one of my favorite websites among the group of all tutsplus websites!', $matches); /** $matches value: Array ( [0] => Array ( [0] => www.weixiaolive.com/en ) [1] => Array ( [0] => tutsplus ) ) **/
示例 3:查找图像路径
<?php preg_match_all('/< *img[^>]*src *= *["\']?([^"\']*)/i', '<img src="http://example.com/image1.jpg" /> <img src="http://example.com/image2.jpg" />', $matches); /** $matches value: Array ( [0] => Array ( [0] => <img src="http://example.com/image1.jpg [1] => <img src="http://example.com/image2.jpg ) [1] => Array ( [0] => http://example.com/image1.jpg [1] => http://example.com/image2.jpg ) ) **/
示例 4:查找特定的 HTML 标签
<?php preg_match_all('#<div class="tutsplus">(.*?)</div>#s', '<div>Some text.</div> <div class="tutsplus">Special text.</div><div>Some more text.</div> <div class="tutsplus">More special text.</div>', $matches); /** $matches value: Array ( [0] => Array ( [0] => <div class="tutsplus">Special text.</div> [1] => <div class="tutsplus">More special text.</div> ) [1] => Array ( [0] => Special text. [1] => More special text. ) ) **/
需要注意的是,当您使用该preg_match_all函数时,该$matches变量会使用多维数组进行初始化。该$matches[0]元素使用与完整模式匹配的所有值的数组进行初始化。另一方面,该$matches[1]元素使用与第一个带括号的子模式匹配的值数组进行初始化,该$matches[2]元素包含与第二个带括号的子模式匹配的值数组,依此类推。
该preg_match_all函数是一个非常强大的函数,你会在日常的 PHP 开发中经常遇到它。
preg_replace功能_
该preg_replace函数执行正则表达式搜索,并允许您将搜索结果替换为其他字符串。我们来看看它的格式:
preg_replace ( string|array $pattern , string|array $replacement , string|array $subject , int $limit = -1 , int &$count = null );
第一个参数是您要搜索的模式。它可以是字符串或字符串数组。在第二个参数中,您可以传递一个字符串或一个字符串数组来替换它。请务必注意,如果您将$replacement参数作为字符串提供并且$pattern参数包含数组,则所有匹配的模式都将替换为$replacement字符串。另一方面,如果您同时提供$pattern和$replacement作为数组,这将是一对一的搜索和替换操作。
第三个参数是要搜索和替换的字符串或字符串数组。更进一步,第四个参数允许您指定要为每个模式执行的最大替换次数。最后,最后一个参数允许您传递一个变量,该变量将填充完成的替换次数。
现在让我们看几个例子。
示例 1:简单的字符串替换
<?php $sourceString = "Let's fix the spelling of Tutplus in this sentence. The Tutplus is a great resource for online learning!"; echo preg_replace('/Tutplus/i', 'tutsplus', $sourceString); /** OUTPUT: Let's fix the spelling of tutsplus in this sentence. The tutsplus is a great resource for online learning! **/
在此示例中,我们只是替换文本中出现的单个单词。
示例 2:用数组替换
<?php $sourceString = "This text contains shortcodes like {LOGO}, {HOMEPAGE_URL} and {CONTACT_EMAIL}. It should be replaced with the actual content before it's rendered."; echo preg_replace( array('/{LOGO}/i', '/{HOMEPAGE_URL}/i', '/{CONTACT_EMAIL}/i'), array('<img src="http://example.com/logo.png"', 'http://example.com', 'admin@example.com'), $sourceString ); /** OUTPUT: This text contains shortcodes like <img src="http://example.com/logo.png", http://example.com and admin@example.com. It should be replaced with the actual content before it's rendered. **/
在这个例子中,我们有一个要匹配的模式列表和一个替换每个模式的内容列表。这类似于简码在wordpress中的工作方式。
示例 3:将相对 URL 转换为绝对 URL
<?php $sourceString = '<p>Some text here.</p><p><img src="images/logo.jpg" /></p><p> <img src="images/logo2.jpg" /></p><p>More text here.</p>'; $pattern = '/<img(\s+)src="images\//i'; $replace = '<img src="http://example.com/images/'; echo preg_replace($pattern, $replace, $sourceString); /** OUTPUT: <p>Some text here.</p><p><img src="http://example.com/images/logo.jpg" /></p><p> <img src="http://example.com/images/logo2.jpg" /></p><p>More text here.</p> **/
在此示例中,我们将相对图像 URL 替换为绝对 URL。请注意,这有点简单——它不应该是如何将相对 URL 重写为绝对 URL 的真实演示。
示例 4:替换为引用
<?php $sourceString = '<div>Some text.</div><div class="tutsplus">Special text.</div> <div>Some more text.</div>'; $pattern = '#<div class="tutsplus">(.*?)</div>#s'; $replace = '<b>$1</b>'; echo preg_replace($pattern, $replace, $sourceString); /** OUTPUT: <div>Some text.</div><b>Special text.</b><div>Some more text.</div> **/
在最后一个示例中,我们使用$1了变量中的引用$replace,它引用了第一个带括号的模式捕获的文本。这样,您还可以在替换字符串中使用捕获的文本本身。
在讨论preg_replace函数时,您还应该知道还有另一个函数 ,preg_filter它与函数的工作方式相同preg_replace,只是它只返回与模式匹配的主题。
preg_grep功能_
该preg_grep函数允许您对值数组执行模式匹配。
该preg_grep函数的语法如下所示:
preg_grep ( string $pattern , array $array , int $flags = 0 );
第一个参数是您要搜索的模式,第二个参数是输入数组。
preg_grep当您有一个项目数组并且想要提取具有特定模式的项目时,该函数非常有用。
让我们快速浏览以下示例。
<?php $arrWebsites = [ "www.weixiaolive.com/en", "example.com", "someotherdomain.com", "webdesign.www.weixiaolive.com", "business.www.weixiaolive.com" ]; $result = preg_grep("/(.*)[.]tutsplus[.](.*)/i", $arrWebsites); print_r($result); /** OUTPUT: Array ( [0] => www.weixiaolive.com/en [3] => webdesign.www.weixiaolive.com [4] => business.www.weixiaolive.com ) **/ ?>
在上面的示例中,我们使用了该preg_grep功能来过滤属于该tutsplus组的网站。
功能_preg_replace_callback
该preg_replace_callback函数与该函数几乎相同preg_replace,只是您需要在第二个参数中指定一个回调函数而不是替换字符串。
让我们转换preg_replace我们之前讨论过的例子之一preg_replace_callback。
<?php $sourceString = '<div>Some text.</div><div class="tutsplus">Special text.</div><div>Some more text.</div>'; $pattern = '#<div class="tutsplus">(.*?)</div>#s'; echo preg_replace_callback($pattern, function ($matches) {return '<b>' . $matches[1] . '</b>';}, $sourceString); /** OUTPUT: <div>Some text.</div><b>Special text.</b><div>Some more text.</div> **/ ?>
如您所见,我们在函数的第二个参数中提供了一个匿名preg_replace_callback函数。它将接收$matches数组中捕获的文本元素,您可以使用它进行替换。
preg_split功能_
该preg_split函数允许您通过正则表达式拆分字符串。
让我们看一下preg_split函数的语法。
preg_split ( string $pattern , string $subject , int $limit = -1 , int $flags = 0 );
第一个参数是您要搜索的模式,第二个参数是输入字符串。
让我们看一下以下示例以了解其工作原理。
<?php $sourceString = "This is a long text. It contains a lot of line breaks. Let's split it with the preg_split function."; $result = preg_split ("/\\n/", $sourceString); print_r($result); /** OUTPUT: Array ( [0] => This is a long text. [1] => It contains a lot of line breaks. [2] => Let's split it with the preg_split function. ) **/ ?>
在这里您可以看到我们使用了匹配换行符的模式,并且输出返回文本的各个行。
结论
今天,我们讨论了 PHP 中的正则表达式函数。我们探索了各种preg_*家庭功能,以及现实世界的例子来了解它们是如何工作的。
- preg_match功能_
- 示例 1:简单字符串匹配
- 示例 2:简单模式匹配
- 示例 3:查找图像路径
- 示例 4:查找特定的 html 标签
- preg_match_all功能_
- 示例 1:简单字符串匹配
- 示例 2:简单模式匹配
- 示例 3:查找图像路径
- 示例 4:查找特定的 HTML 标签
- preg_replace功能_
- 示例 1:简单的字符串替换
- 示例 2:用数组替换
- 示例 3:将相对 URL 转换为绝对 URL
- 示例 4:替换为引用
- preg_grep功能_
- 功能_preg_replace_callback
- preg_split功能_