欢迎访问宙启技术站
智能推送

PHP中的10个正则表达式函数,掌握匹配技巧

发布时间:2023-06-25 06:57:32

正则表达式是一种用于规定字符串匹配模式的语言,它可以验证输入的字符串是否符合规定的模式,也可以在字符串中找到符合要求的子串。在 PHP 中,正则表达式的使用非常普遍,以下是 PHP 中常用的 10 个正则表达式函数及使用技巧。

1. preg_match()

preg_match() 函数用于在字符串中搜索指定的模式,如果找到匹配,则返回 true ,否则返回 false。其语法为:

preg_match(pattern, subject);

其中, pattern 是要搜索的模式, subject 是待搜索的字符串。

使用样例:

$str = "Hello PHP";
if (preg_match("/PHP/", $str)) {
  echo "Found PHP!";
} else {
  echo "Not found PHP!";
}

输出结果为: Found PHP。

2. preg_replace()

preg_replace() 函数用于搜索和替换字符串中的内容。其语法为:

preg_replace(pattern, replacement, subject);

其中, pattern 是要搜索的模式, replacement 是替换的字符串, subject 是要搜索和替换的字符串。

使用样例:

$str = "Hello PHP";
$newstr = preg_replace("/PHP/", "World", $str);
echo $newstr;

输出结果为: Hello World。

3. preg_split()

preg_split() 函数用于按照指定的模式将字符串分割成数组。其语法为:

preg_split(pattern, subject);

其中, pattern 是要分割的模式, subject 是要分割的字符串。

使用样例:

$str = "Hello,World,PHP";
$arr = preg_split("/,/", $str);
print_r($arr);

输出结果为:

Array
(
    [0] => Hello
    [1] => World
    [2] => PHP
)

4. preg_grep()

preg_grep() 函数用于从数组中搜索与指定模式匹配的元素。其语法为:

preg_grep(pattern, input);

其中, pattern 是要搜索的模式, input 是待搜索的数组。

使用样例:

$arr = array("Hello", "World", "PHP");
$newarr = preg_grep("/P/", $arr);
print_r($newarr);

输出结果为:

Array
(
    [2] => PHP
)

5. preg_last_error()

preg_last_error() 函数用于返回最后一个正则表达式操作相关的错误代码。其语法为:

preg_last_error( );

使用样例:

$str = "Hello World?";
if (!preg_match("/P/", $str)) {
  echo "Error code: " . preg_last_error();
}

输出结果为: Error code: 2。

6. preg_match_all()

preg_match_all() 函数用于返回符合模式的所有匹配项。其语法为:

preg_match_all(pattern, subject, matches);

其中, pattern 是要搜索的模式, subject 是待搜索的字符串, matches 是一个可选的指向输出数组的引用。

使用样例:

$str = "Hello PHP, Hello World, Hello Everyone!";
preg_match_all("/Hello (.*?)!/i", $str, $matches);
print_r($matches);

输出结果为:

Array
(
    [0] => Array
        (
            [0] => Hello PHP!
            [1] => Hello World!
            [2] => Hello Everyone!
        )

    [1] => Array
        (
            [0] => PHP
            [1] => World
            [2] => Everyone
        )

)

7. preg_quote()

preg_quote() 函数用于转义正则表达式中的字符。其语法为:

preg_quote(str [,delimiter]);

其中, str 是要转义的字符, delimiter 是可选的分隔符。

使用样例:

$str = "Hello | PHP";
$pattern = "/". preg_quote("|") ."/i";
if (preg_match($pattern, $str)) {
  echo "Found |!";
} else {
  echo "Not found |!";
}

输出结果为: Found |。

8. preg_replace_callback()

preg_replace_callback() 函数用于使用回调函数替换字符串中的内容。其语法为:

preg_replace_callback(pattern, callback, subject, [limit]);

其中, pattern 是要搜索的模式, callback 是一个回调函数, subject 是要搜索和替换的字符串, limit 是可选的限制回调执行的最大次数。

使用样例:

$str = "Hello PHP";
$newstr = preg_replace_callback("/PHP/", function($matches) {
  return strtoupper($matches[0]);
}, $str);
echo $newstr;

输出结果为: Hello PHP。

9. preg_filter()

preg_filter() 函数用于搜索和替换数组中的元素,类似于 preg_replace() 函数,但返回匹配部分,而原始输入不变。其语法为:

preg_filter(pattern, replacement, subject);

其中, pattern 是要搜索的模式, replacement 是替换的字符串, subject 是要搜索和替换的数组。

使用样例:

$arr = array("Hello", "PHP");
$newarr = preg_filter("/PHP/", "World", $arr);
print_r($newarr);
print_r($arr);

输出结果为:

Array
(
    [1] => World
)
Array
(
    [0] => Hello
    [1] => PHP
)

10. preg_quote()

preg_quote() 函数用于转义正则表达式中的字符。其语法为:

preg_quote(str [,delimiter]);

其中, str 是要转义的字符, delimiter 是可选的分隔符。

使用样例:

$str = "Hello | PHP";
$pattern = "/". preg_quote("|") ."/i";
if (preg_match($pattern, $str)) {
  echo "Found |!";
} else {
  echo "Not found |!";
}

输出结果为: Found |。

以上是 PHP 中常用的 10 个正则表达式函数及使用技巧。在实际编程中,我们可以根据不同的需求,运用这些函数进行字符串搜索、替换、分割、过滤等操作,从而快速高效地完成任务。