<p>In the previous article, I brought you "<a href="https://www.php.cn/php-ask-483648.html" target="_blank">How to perform regular expression search and replacement in PHP? </a>》, 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! </p>
<p><img src="https://img.php.cn/upload/article/000/000/067/617f7f6f7bab3508.jpg" alt="How to split and escape strings with PHP regular expressions?" ></p>
<p>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 <code>preg_match() </code> function, <code>preg_match_all()</code> function, <code>preg_grep()</code> function and <code>preg_replace()</code> function It can be achieved. The main purpose of using regular expressions is to achieve powerful functions in a simple way. </p>
<p>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. <br></p>
<p><code><strong><span style="font-size: 20px;">preg_split() </span></strong></code><strong><span style="font-size: 20px;">Function </span></strong></p>
<p>at In PHP, the <code>preg_split()</code> function splits a string through a regular expression. The syntax format of this function is as follows: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )</pre><div class="contentsignin">Copy after login</div></div><p> What needs to be noted is: <br/></p><p><code>$pattern</code> represents the pattern used for matching, that is, a regular expression; <code>$subject</code> represents the string to be separated; <code>$limit</code> 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 <code>-1</code>, <code>0</code> or <code>NULL </code>, it means "no limit", and it is recommended to use NULL. </p><p><code>$flags</code> is an optional parameter with 3 values. If set to <code>PREG_SPLIT_NO_EMPTY</code>, preg_split() will return the separated non-empty part. If set to <code>PREG_SPLIT_DELIM_CAPTURE</code>, bracket expressions in delimited patterns will be captured and returned. If set to <code>PREG_SPLIT_OFFSET_CAPTURE</code>, the string offset will be appended to the return for each occurrence of a match. </p><p>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. </p><p>Next let’s take a look at the usage of the preg_split() function through an example. The example is as follows: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><?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) );
?></pre><div class="contentsignin">Copy after login</div></div><p>Output result: <br/></p><p><img src="https://img.php.cn/upload/image/234/429/279/1635748311992399.png" title="1635748311992399.png" alt="How to split and escape strings with PHP regular expressions?"/></p><p>The example is as follows: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><?php
$str = 'father mother andyou';
var_dump($str);
$chars = preg_split('/ /', $str, -1, PREG_SPLIT_OFFSET_CAPTURE);
var_dump($chars);
?></pre><div class="contentsignin">Copy after login</div></div><p>Output result: <br/></p><p><img src="https://img.php.cn/upload/image/406/631/908/1635750264433447.png" title="1635750264433447.png" alt="How to split and escape strings with PHP regular expressions?"/></p><p>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. </p><p><code><strong><span style="max-width:90%">PHP preg_quote()</span></strong></code><strong>## Function <span style="font-size: 20px;"></span></strong></p>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. <p></p>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. <p></p>PHP <p>preg_quote()<code> The function is used to escape the regular expression string, that is, add a backslash </code>\<code> in front of the special character. This function The syntax format is as follows: </code><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">preg_quote($str [, $delimiter = NULL])</pre><div class="contentsignin">Copy after login</div></div></p> It should be noted that: <p><br/></p><p>$str<code> represents a regular expression string; </code>$delimiter<code> Is an optional parameter, additional characters that need to be escaped. If the </code> $delimiter <code> parameter is specified, the specified characters will also be escaped. This is typically used to escape delimiters used by the PCRE function. </code> / <code> are the most common delimiters. </code></p><p>preg_quote() <code>The function will add a backslash before each regular expression character provided by the parameter </code> $str <code>. This is typically used when some runtime string needs to be matched as a regular expression. </code><p>正则表达式特殊字符有:<code>.</code> <code>\</code> <code>+</code> <code>*</code> <code>?</code> <code>[</code> <code>^</code> <code>]</code> <code>$</code> <code>(</code> <code>)</code> <code>{</code> <code>}</code> <code>=</code> <code>!</code> <code><</code> <code>></code> <code>|</code> <code>:</code> <code>-</code> 。要清楚<code>/</code>不是正则表达式特殊字符。</p><p>接下来我们通过示例来看一下使用 preg_quote() 函数对字符串进行转义,示例如下:</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><?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 );
?></pre><div class="contentsignin">Copy after login</div></div><p>输出结果:<br/></p><p><img src="https://img.php.cn/upload/image/801/582/141/1635749241170496.png" title="1635749241170496.png" alt="How to split and escape strings with PHP regular expressions?"/></p><p>示例如下:</p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><?php
$keywords = '$40 for a g3/400';
$keywords = preg_quote($keywords, '/');
echo $keywords; // 返回 \$40 for a g3\/400
?></pre><div class="contentsignin">Copy after login</div></div><p>输出结果:<br></p>
<p><img src="https://img.php.cn/upload/image/733/302/724/1635750498161054.png" title="1635750498161054.png" alt="How to split and escape strings with PHP regular expressions?"></p>
<p>上述示例中,便是通过preg_quote() 函数用来对正则表达式字符串进行转义。</p>
<p>大家如果感兴趣的话,可以点击《<a href="https://www.php.cn/course/list/29/type/2.html" target="_blank">PHP视频教程</a>》、《<a href="https://www.php.cn/course/59.html" target="_blank">正则表达式手册</a>》进行更多关于PHP和正则表达式知识的学习。</p>
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!