Home > Backend Development > PHP Tutorial > Introduction to PHP escape metacharacter sets (code examples)

Introduction to PHP escape metacharacter sets (code examples)

藏色散人
Release: 2023-04-05 15:44:01
Original
2311 people have browsed it

quotemeta()函数转义元字符集。quotemeta()函数是在PHP一个内置函数,其接受字符串作为参数并返回其具有在一些预定义的字符前添加在字符串反斜线的字符串。

Introduction to PHP escape metacharacter sets (code examples)

预定义字符为:

句点(.)
反斜杠(\)
加号(+)
星号(*)
问号(?)
方括号([])
插入符号(^)
美元符号($)
括号(())
Copy after login

语法:

 quotemeta($string)
Copy after login

参数:此函数只接受一个强制参数$string。此参数指定要在上述预定义字符前添加反斜杠的字符串。

返回值:它通过在$string参数中预定义的字符前添加反斜杠来返回一个字符串。

示例1:当字符串有' ?和“$”预定义字符

<?php
$str = "PHP$ and Java?";

//通过在预定义字符“$”和“?”前面添加反斜杠来打印字符串。
echo(quotemeta($str));
Copy after login

输出:

PHP\$ and Java\?
Copy after login

例2:当字符串有' * '和“+”预定义字符

<?php
$str = "+PHP* and Java.";

// 通过在预定义字符前添加反斜杠来打印字符串
echo(quotemeta($str));
Copy after login

输出:

\+PHP\* and Java\.
Copy after login

示例3:当字符串有[]和()作为预定义字符时。

<?php

$str = "[]PHP and Java()";

// 通过在预定义的字符、[]括号和()主题前添加反斜杠来打印字符串
echo(quotemeta($str));
Copy after login

输出:

\[\]PHP and Java\(\)
Copy after login

示例4:当字符串有插入符号(^)作为预定义字符时。

<?php 

$str = "2 ^ 2 = 4"; 
  
// 通过在预定义字符前添加反斜杠来打印字符串
echo(quotemeta($str));
Copy after login

输出:

2 \^ 2 = 4
Copy after login

相关推荐:《PHP教程

本篇文章就是关于PHP转义元字符集即PHP quotemeta()函数的介绍,希望对需要的朋友有所帮助!

The above is the detailed content of Introduction to PHP escape metacharacter sets (code examples). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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