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

Introduction to PHP escape metacharacter sets (code examples)

藏色散人
藏色散人Original
2019-03-14 10:16:272211browse

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

Introduction to PHP escape metacharacter sets (code examples)

预定义字符为:

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

语法:

 quotemeta($string)

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

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

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

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

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

输出:

PHP\$ and Java\?

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

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

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

输出:

\+PHP\* and Java\.

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

<?php

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

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

输出:

\[\]PHP and Java\(\)

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

<?php 

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

输出:

2 \^ 2 = 4

相关推荐:《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!

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