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

PHP中最有用的10个正则表达式函数

发布时间:2023-06-08 13:17:18

正则表达式在PHP中是一项重要的工具,可以帮助开发人员快速有效的处理文本。PHP内置了许多正则表达式函数,以下是其中最有用的10个。

1. preg_match()函数

preg_match()函数用于在字符串中匹配正则表达式,返回匹配结果数组或false。

例如:

$str = "The quick brown fox jumps over the lazy dog.";

$pattern = '/quick/';

preg_match($pattern, $str, $matches);

print_r($matches);

输出:

Array

(

    [0] => quick

)

2. preg_match_all()函数

preg_match_all()函数用于在字符串中查找所有匹配的正则表达式,返回匹配结果数组。

例如:

$str = "The quick brown fox jumps over the lazy dog.";

$pattern = '/the/';

preg_match_all($pattern, $str, $matches);

print_r($matches);

输出:

Array

(

    [0] => Array

        (

            [0] => the

            [1] => the

        )

)

3. preg_replace()函数

preg_replace()函数用于使用正则表达式在字符串中替换文本,返回替换后的字符串。

例如:

$str = "The quick brown fox jumps over the lazy dog.";

$pattern = '/quick/';

$replacement = 'slow';

$new_str = preg_replace($pattern, $replacement, $str);

echo $new_str;

输出:

The slow brown fox jumps over the lazy dog.

4. preg_split()函数

preg_split()函数用于使用正则表达式在字符串中分隔文本,返回分隔后的字符串数组。

例如:

$str = "The quick brown fox jumps over the lazy dog.";

$pattern = '/\W+/';

$arr = preg_split($pattern, $str);

print_r($arr);

输出:

Array

(

    [0] => The

    [1] => quick

    [2] => brown

    [3] => fox

    [4] => jumps

    [5] => over

    [6] => the

    [7] => lazy

    [8] => dog

    [9] => 

)

5. preg_grep()函数

preg_grep()函数用于在数组中模式匹配,返回所有匹配的数组元素。

例如:

$arr = array('apple', 'banana', 'orange', 'peach');

$pattern = '/an/';

$new_arr = preg_grep($pattern, $arr);

print_r($new_arr);

输出:

Array

(

    [1] => banana

    [2] => orange

)

6. preg_quote()函数

preg_quote()函数用于转义正则表达式中的元字符。

例如:

$str = "http://www.example.com/";

$pattern = preg_quote(".com/");

$replacement = ".cn/";

$new_str = preg_replace($pattern, $replacement, $str);

echo $new_str;

输出:

http://www.example.cn/

7. preg_match_callback()函数

preg_match_callback()函数用于使用回调函数处理正则表达式匹配结果。

例如:

$str = "The quick brown fox jumps over the lazy dog.";

$pattern = '/quick/';

$new_str = preg_replace_callback($pattern, function($matches){

    return strtoupper($matches[0]);

}, $str);

echo $new_str;

输出:

The QUICK brown fox jumps over the lazy dog.

8. preg_last_error()函数

preg_last_error()函数用于返回最后一个正则表达式操作的错误代码。

例如:

preg_match('/(?(?=\d+.)(\d+)\.)?(.*)/', '5.4', $matches);

echo preg_last_error();

输出:

0

9. preg_filter()函数

preg_filter()函数用于使用正则表达式在数组中搜索并替换元素,返回替换后的新数组。

例如:

$arr = array('apple', 'banana', 'orange', 'peach');

$pattern = '/an/';

$replacement = 'AN';

$new_arr = preg_filter($pattern, $replacement, $arr);

print_r($new_arr);

输出:

Array

(

    [0] => apple

    [1] => bANANA

    [2] => orANGe

    [3] => peach

)

10. preg_split()函数的flags参数

preg_split()函数的flags参数用于控制分隔结果以及多行匹配。

例如:

$str = "The quick

brown fox jumps over

the lazy dog.";

$pattern = '/\s/';

$arr = preg_split($pattern, $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

print_r($arr);

输出:

Array

(

    [0] => The

    [1] => quick

    [2] => brown

    [3] => fox

    [4] => jumps

    [5] => over

    [6] => the

    [7] => lazy

    [8] => dog.

)