Home > Backend Development > PHP Problem > How to escape regular expressions in PHP

How to escape regular expressions in PHP

autoload
Release: 2023-03-08 22:28:01
Original
2883 people have browsed it

How to escape regular expressions in PHP

    本文主要讲述了如何利用 preg_quote() 函数用来对正则表达式字符串进行转义

1.语法:

string preg_quote ( string $str [, string $delimiter = NULL ] )
Copy after login
  • $str: 输入字符串。

  • $delimiter: 如果指定了可选参数 delimiter,它也会被转义。这通常用于 转义 PCRE 函数使用的分隔符。 / 是最通用的分隔符。

preg_quote()需要参数 str 并向其中 每个正则表达式语法中的字符前增加一个反斜线。 这通常用于你有一些运行时字符串 需要作为正则表达式进行匹配的时候。

目前支持的正则表达式特殊字符有: . \ + * ? [ ^ ] $ ( ) { } = ! < > | : - #

PS:字符 # 7.3.0版本被增加为需要转义的。

2.示例:

<?php

$keywords = &#39;$40 for a g3/400&#39;;
$keywords = preg_quote($keywords, &#39;/&#39;);
echo $keywords; // 返回 \$40 for a g3\/400
?>
Copy after login

推荐:《php视频教程》《 php教程》

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

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template