Home>Article>Backend Development> How to split and escape strings with PHP regular expressions?

How to split and escape strings with PHP regular expressions?

WBOY
WBOY Original
2021-11-01 15:46:23 3458browse

In the previous article, I brought you "How to perform regular expression search and replacement in PHP?》, which introduces in detail the relevant knowledge of performing regular expression search and replacement in PHP. In this article, we will continue to look at the relevant knowledge of string segmentation and escaping in regular expressions. I hope it will be helpful to everyone!

How to split and escape strings with PHP regular expressions?

In previous articles we learned about performing global regular expression matching in PHP, detecting array elements that match a given pattern, performing a regular expression search and We can replace such operations through thepreg_match()function,preg_match_all()function,preg_grep()function andpreg_replace()function It can be achieved. The main purpose of using regular expressions is to achieve powerful functions in a simple way.

There are many operations between regular expressions and strings, including using regular expressions to split strings. Next, let’s take a look at how we can use character expressions to Split the string.

preg_split() Function

at In PHP, thepreg_split()function splits a string through a regular expression. The syntax format of this function is as follows:

array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )

What needs to be noted is:

$patternrepresents the pattern used for matching, that is, a regular expression;$subjectrepresents the string to be separated;$limitis an optional parameter. If specified, limit-separated substrings will be limited to at most limit, and the last substring will contain any remainder. When the limit value is-1,0orNULL, it means "no limit", and it is recommended to use NULL.

$flagsis an optional parameter with 3 values. If set toPREG_SPLIT_NO_EMPTY, preg_split() will return the separated non-empty part. If set toPREG_SPLIT_DELIM_CAPTURE, bracket expressions in delimited patterns will be captured and returned. If set toPREG_SPLIT_OFFSET_CAPTURE, the string offset will be appended to the return for each occurrence of a match.

This will change each element in the returned array so that each element becomes a substring separated by the 0th element and the 1st element is the offset of the substring in the subject An array of quantities. The return value is an array composed of substrings obtained after splitting the subject string using pattern.

Next let’s take a look at the usage of the preg_split() function through an example. The example is as follows:

"; $subject = 'PHP中文网://m.sbmmt.com/, baidu百度:http://www.baidu.com/'; $pattern = '/[\s,:]+/'; print_r( preg_split($pattern, $subject) ); print_r( preg_split($pattern, $subject, 3) ); ?>

Output result:

How to split and escape strings with PHP regular expressions?

The example is as follows:

Output result:

How to split and escape strings with PHP regular expressions?

As can be seen from the above example, the preg_split() function can be used to complete a true expression to split the string. Next let's take a look at how to escape regular expressions.

PHP preg_quote()## Function

Common characters include All printable and nonprintable characters not explicitly designated as metacharacters, including all uppercase and lowercase letters, numbers, punctuation, and some symbols. The simplest regular expression is a single ordinary character used to compare search strings. For example, the single-character regular expression /A/ will always match the letter A.

In addition to ordinary characters, regular expressions can also contain "metacharacters". Metacharacters can be divided into single-character metacharacters and multi-character metacharacters. For example, the metacharacter \d, which matches numeric characters.

PHP

preg_quote()The function is used to escape the regular expression string, that is, add a backslash\in front of the special character. This function The syntax format is as follows:

preg_quote($str [, $delimiter = NULL])

It should be noted that:


$strrepresents a regular expression string;$delimiterIs an optional parameter, additional characters that need to be escaped. If the$delimiterparameter is specified, the specified characters will also be escaped. This is typically used to escape delimiters used by the PCRE function./are the most common delimiters.

preg_quote()The function will add a backslash before each regular expression character provided by the parameter$str. This is typically used when some runtime string needs to be matched as a regular expression.

正则表达式特殊字符有:.\+*?[^]$(){}=!>|:-。要清楚/不是正则表达式特殊字符。

接下来我们通过示例来看一下使用 preg_quote() 函数对字符串进行转义,示例如下:

"; echo preg_quote($url, "/") . "
"; $textbody = "baidu百度 is *very* good to study."; $word = "*very*"; echo preg_replace( "/".preg_quote($word)."/", "".$word."", $textbody ); ?>

输出结果:

How to split and escape strings with PHP regular expressions?

示例如下:

输出结果:

How to split and escape strings with PHP regular expressions?

上述示例中,便是通过preg_quote() 函数用来对正则表达式字符串进行转义。

大家如果感兴趣的话,可以点击《PHP视频教程》、《正则表达式手册》进行更多关于PHP和正则表达式知识的学习。

The above is the detailed content of How to split and escape strings with PHP regular expressions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn