Home > Article > Backend Development > How to split and escape strings with PHP regular expressions?
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!
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 the preg_match()
function, preg_match_all()
function, preg_grep()
function and preg_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.
<strong><span style="font-size: 20px;">preg_split() </span></strong>
Function
at In PHP, the preg_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:
$pattern
represents the pattern used for matching, that is, a regular expression; $subject
represents the string to be separated; $limit
is 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
, 0
or NULL
, it means "no limit", and it is recommended to use NULL.
$flags
is an optional parameter with 3 values. If set to PREG_SPLIT_NO_EMPTY
, preg_split() will return the separated non-empty part. If set to PREG_SPLIT_DELIM_CAPTURE
, bracket expressions in delimited patterns will be captured and returned. If set to PREG_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:
<?php echo "<pre class="brush:php;toolbar:false">"; $subject = 'PHP中文网:http://www.php.cn/, baidu百度:http://www.baidu.com/'; $pattern = '/[\s,:]+/'; print_r( preg_split($pattern, $subject) ); print_r( preg_split($pattern, $subject, 3) ); ?>
Output result:
The example is as follows:
<?php $str = 'father mother andyou'; var_dump($str); $chars = preg_split('/ /', $str, -1, PREG_SPLIT_OFFSET_CAPTURE); var_dump($chars); ?>
Output result:
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.
<strong><span style="font-size: 20px;">PHP preg_quote()</span></strong>
## Function
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:
$str represents a regular expression string;
$delimiter Is an optional parameter, additional characters that need to be escaped. If the
$delimiter parameter 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.
正则表达式特殊字符有:.
\
+
*
?
[
^
]
$
(
)
{
}
=
!
<code>>
|
:
-
。要清楚/
不是正则表达式特殊字符。
接下来我们通过示例来看一下使用 preg_quote() 函数对字符串进行转义,示例如下:
<?php $url = 'http://www.baidu.com/'; echo preg_quote($url) . "<br />"; echo preg_quote($url, "/") . "<br />"; $textbody = "baidu百度 is *very* good to study."; $word = "*very*"; echo preg_replace( "/".preg_quote($word)."/", "<i>".$word."</i>", $textbody ); ?>
输出结果:
示例如下:
<?php $keywords = '$40 for a g3/400'; $keywords = preg_quote($keywords, '/'); echo $keywords; // 返回 \$40 for a g3\/400 ?>
输出结果:
上述示例中,便是通过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!