Home >Backend Development >PHP Problem >How to escape regular special characters in php

How to escape regular special characters in php

藏色散人
藏色散人Original
2020-10-20 09:18:134595browse

php method of escaping regular special characters: first create a PHP sample file; then define a string; finally escape the special characters through backslashes and parse the variables.

How to escape regular special characters in php

Recommended: "PHP Video Tutorial"

Question

Recently used PHP regular expressions , I encountered some problems when writing expression strings. I don’t know when I need to use the backslash "\". Now I will do some sorting.

PHP string

There are 4 ways to define a PHP string:

  • Single quotes
  • Double quotes
  • heredoc syntax structure
  • nowdoc syntax structure (since PHP 5.3.0)

When defining a string, special characters will be escaped and variables will be parsed.

Special characters include:

##ItemContentRemarkLine feed\nLF or 0x0A##Carriage EnterHorizontal tabVertical tab Page changeBackslash##Dollar sign\$Double quotes\"Single quotes\' Octal characters\[0-7]{1,3}Hexadecimal characters\x[0-9A-Fa-f]{1,2}

See PHP Manual for details on this part. Here is a brief description:

In single quotes, escape single quotes (') and backslashes (\), and output the original meaning of other characters;

In double quotes, special characters except double quotes (\") are escaped and variables are parsed.

Therefore, in these two common string definitions, backslashes are Bars (\) always need to be escaped.

PHP Regular Expressions

PHP provides two sets of regular expression function libraries: [Reference]

One set is Provided by the PCRE (Perl Compatible Regular Expression) library. Use functions named with the prefix "preg_";
A set of functions provided by POSIX (Portable Operating System Interface of Unix) extensions (PHP default). Use "ereg_" Function named for the prefix;

Regular expressions provide the functions of matching, replacing, and splitting.

Currently only perl-style regular functions are used. The following is only for this type of strings Description.

Perl-style regular expressions are required to be contained in delimiters ("/" or "#"), as follows: [Reference]

$str = 'http://www.youku.com/show_page/id_ABCDEFG.html';
$regex = '/^http:\/\/([\w.]+)\/([\w]+)\/([\w]+)\.html$/i';
$regex = '#^http://([\w.]+)/([\w]+)/([\w]+)\.html$#i';

In order to split a URL, the $regex variable defines a regular expression string:

^http://([\w.] )/([\w] )/([\w ] ).html$

This string contains some special characters, such as "/" and ".", which need to be escaped and defined with backslash ("\") in Perl style, as follows :

^http:\/\/([\w.] )\/([\w] )\/([\w] )\.html$

At the same time this character The string needs to be included in the delimiter, so there are the above two types of expressions; if "#" is used as the delimiter, the "/" does not need to be escaped.

Regular Expression escaping

In regular expressions, if you want to match the following single characters, you need to use backslash ("\") to escape:

"\", "? ","*","^","$"," ","(",")","|","{","["

In Perl-style regular expressions , if it matches the following single character, or a single character not matched above, it also needs to be escaped with a backslash ("\"):

^=}]/:a8093152e673feb7aba1828c43532094.'"

Summary

To write a correct Perl-style regular expression string, three steps are required:

Write a correct regular expression and pay attention to the conversion of special characters Definition

Put it into the delimiter and escape it using Perl-style escaping rules

Escape the content of the above string according to the way PHP string definition

\r CR or 0x0D
\t HT or 0x09
\v VT or 0x0B
\f FF or 0x0C
\\

The above is the detailed content of How to escape regular special characters in php. 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