search
HomeBackend DevelopmentPHP ProblemHow to escape single quotes in php

How to escape single quotes in php

Mar 04, 2021 pm 05:51 PM
phpapostrophe

php method to escape single quotes: Use the [addslashes()] function to add a backslash before the specified predefined character. The syntax is [addslashes(string)]. String is required and is required to be checked. String.

How to escape single quotes in php

The operating environment of this tutorial: Windows 7 system, PHP version 5.6, DELL G3 computer.

php method of escaping single quotes:

PHP addslashes() function

Definition and usage

addslashes()The function adds a backslash before the specified predefined character.

These predefined characters are:

  • Single quote (')

  • Double quote (")

  • Backslash(\)

  • NULL

##Syntax

addslashes(string)

Parameter Description

string Required. Specifies the string to be checked.

Tips and comments

Tips: This function can be used to store Prepare appropriate strings for strings in the database and database query statements.

Note:

By default, the PHP directive

magic_quotes_gpc is on for all GET , POST and COOKIE data automatically run addslashes().

Do not use addslashes() on strings that have been

magic_quotes_gpc escaped, because this will cause double-layer escaping. Encountered In this case, you can use the function get_magic_quotes_gpc() to detect.

Example

In this example, we want to add the Predefined adding backslash:

<?php$str = "Who&#39;s John Adams?";echo $str . " This is not safe in a database query.<br />";echo
addslashes($str)
. " This is safe in a database query.";?>

Output:

Who&#39;s John Adams? This is not safe in a database query.Who\&#39;s John Adams? This is safe in a database query.

Generally used in the following form

if(!(get_magic_quotes_gpc())) {
$_GET = addslashes($_GET);
$_POST = addslashes($_POST);
    $_COOKIE = addslashes($_COOKIE);
}

Related video recommendations:

PHP video tutorial

The above is the detailed content of How to escape single quotes 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 Article

Hot Tools

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.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)