search
HomeBackend DevelopmentPHP ProblemHow to escape regular special characters in php

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 ("\"):

^=}]/:.'"

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.