Home php教程 php手册 PHP在引号前面添加反斜杠(PHP去除反斜杠)

PHP在引号前面添加反斜杠(PHP去除反斜杠)

Jun 06, 2016 pm 08:27 PM
remove quotation marks slash Add to

PHP在引号前面添加反斜杠的原因及PHP去除反斜杠的办法,三种办法关闭php魔术引号

一般空间商提供的服务器空间默认PHP 指令 magic_quotes_gpc是on的,也就是打开的。这时候就可以用stripslashes() 函数删除自动添加的反斜杠。用法就是:比如包含字符串的变量是$str,那么就用stripslashes() 函数处理一下这个字符串:stripslashes($str),输出的结果就是去掉反斜杠的。

如果遇到输出的结果中包含反斜杠的,可对输出内容用stripslashes()函数处理一下,,即$str=stripslashes($str),保存即可将输出内容中包含的反斜杠去掉。

但是又存在另外一个问题,那就是因为本地PHP 指令 magic_quotes_gpc是off的,如果用这个函数的话,就会把本来正常的反斜杠也去掉了。这样就不是我们所希望的了。

解决办法就是可以使用函数 get_magic_quotes_gpc() 进行检测,如果是打开的状态,那么就去除反斜杠,如果是关闭的状态,则不去除反斜杠。

程序代码如下:

复制代码 代码如下:


 $str=$_POST["str"];           //读取str的内容赋值给$str变量
 if(get_magic_quotes_gpc()){   //如果get_magic_quotes_gpc()是打开的
     $str=stripslashes($str);  //将字符串进行处理
 }

本文于2012年4月25日10:08:03修订如下:

下面介绍三种方法解决这个问题:

1、修改PHP配置文件php.ini
 
这种方法只适于自己有权管理服务器的情况下,如果用的虚拟空间,那就只能采用后两条方法。

在PHP配置文件php.ini中将magic_quotes_gpc、magic_quotes_runtime、magic_quotes_sybase全部设为off。如下所示:

复制代码 代码如下:


magic_quotes_gpc = Off
magic_quotes_runtime = Off
magic_quotes_sybase = Off

2 利用.htaccess文件
 
该方法只在服务器支持htaccess的情况下,现在的服务器一般都会支持的

在程序目录下.htaccess文件中增加下面一句:

php_flag magic_quotes_gpc Off

3 在代码中屏蔽
 
该方法是移植性最强的,不用考虑服务器的配置,只要支持PHP就可以使用。

在所有PHP文件开始处增加下面代码

复制代码 代码如下:


 if(get_magic_quotes_gpc()){
     function stripslashes_deep($value){
         $value=is_array($value)?array_map('stripslashes_deep',$value):stripslashes($value);
         return $value;
     }
     $_POST=array_map('stripslashes_deep',$_POST);
     $_GET=array_map('stripslashes_deep',$_GET);
     $_COOKIE=array_map('stripslashes_deep',$_COOKIE);
     $_REQUEST=array_map('stripslashes_deep',$_REQUEST);
 }

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get rid of arrow icons for shortcuts on Windows 11 How to get rid of arrow icons for shortcuts on Windows 11 Dec 28, 2023 am 10:39 AM

After many users use the latest win11 system, a small arrow appears on the shortcut icon on the desktop, which affects the appearance. Therefore, we have brought you a tutorial to remove the small arrow from the win11 shortcut icon. If you also think it does not look good, come and watch it. Let’s see how it works. How to remove the small shortcut arrow in win11: 1. First, press the "win+r" shortcut keys on the keyboard at the same time. 2. Then open "Run", enter the "regedit" command, and click the "OK" button. 3. Next, enter the "Registry Editor" page and click to open: HKEY_CLASSES_ROOT\lnkfile (can be copied and pasted). 4. After re-entering, right-click "lsShortcut" and select

How to remove watermark with Scanner How to remove watermark with Scanner Mar 05, 2024 pm 05:34 PM

Removing watermarks is a useful tool in the software Scanner. Some users are not sure how to remove watermarks in Scanner. You can click Remove Watermark in Edit PDF on the save interface to close it. Next, the editor will explain Users brought us an introduction to how to remove watermarks. If you are interested, come and take a look! Scanner King usage tutorial How to remove the watermark with Scanner King? Answer: You can click on the save interface to edit the watermark removal in the PDF. Details: 1. Enter the software and click the [Camera] icon. 2. Photograph and scan the documents that need to be watermarked. 3. Click [→] to proceed to the next step. 4. After completing editing, click [✓]. 5. Click [Edit PDF]. 6. Select [Remove Watermark] below.

How to remove shortcut arrow in win11 How to remove shortcut arrow in win11 Jul 05, 2023 pm 02:17 PM

How to remove shortcut arrow in win11? Many Win11 users have created shortcut icons on the system desktop, but there is a small arrow-like symbol on the shortcut icon, which makes the overall icon look very ugly. So is there any corresponding method to remove the small arrow on the Windows 11 system shortcut icon? Many friends don’t know how to operate in detail. The editor below has compiled a tutorial on clearing the small arrow shortcuts on the desktop in Win11. If you are interested, follow the editor and read on! Tutorial on clearing the small arrow shortcuts on the desktop in win11 1. Open the menu bar and find Run or just win+R. 2. Enter regedit and click OK. 3. Locate HKEY_CLASSES_

How to add a TV to Mijia How to add a TV to Mijia Mar 25, 2024 pm 05:00 PM

Many users are increasingly favoring the electronic ecosystem of Xiaomi smart home interconnection in modern life. After connecting to the Mijia APP, you can easily control the connected devices with your mobile phone. However, many users still don’t know how to add Mijia to their homes. app, then this tutorial guide will bring you the specific connection methods and steps, hoping to help everyone in need. 1. After downloading Mijia APP, create or log in to Xiaomi account. 2. Adding method: After the new device is powered on, bring the phone close to the device and turn on the Xiaomi TV. Under normal circumstances, a connection prompt will pop up. Select "OK" to enter the device connection process. If no prompt pops up, you can also add the device manually. The method is: after entering the smart home APP, click the 1st button on the lower left

Tutorial on adding a new hard drive in win11 Tutorial on adding a new hard drive in win11 Jan 05, 2024 am 09:39 AM

When buying a computer, we may not necessarily choose a large hard drive. At this time, if we want to add a new hard drive to win11, we can first install the new hard drive we purchased, and then add partitions to the computer. Tutorial on adding a new hard drive in win11: 1. First, we disassemble the host and find the slot of the hard drive. 2. After finding it, we first connect the "data cable", which usually has a fool-proof design. If it cannot be inserted, just reverse the direction. 3. Then insert the new hard drive into the hard drive slot. 4. After inserting, connect the other end of the data cable to the computer's motherboard. 5. After the installation is completed, you can put it back into the host and turn it on. 6. After booting, we right-click "This Computer" and open "Computer Management" 7. After opening, click "Disk Management" in the lower left corner 8. Then on the right you can

How to remove mosaic How to remove mosaic How to remove mosaic How to remove mosaic Feb 22, 2024 pm 03:22 PM

Use the imitation stamp to pick up the color of the picture and then apply it. Tutorial Applicable Model: Lenovo AIO520C System: Windows 10 Professional Edition: Photoshop 2020 Analysis 1 First enter PhotoShop and open the mosaic picture. 2In the toolbar on the left, find and click Clone Stamp. 3 Then press and hold the Alt key on the keyboard. 4. Move the mouse to pick the color in the picture. 5Then release the Alt key on the keyboard. 6Finally, use the mouse to smear on the mosaic area to eliminate the mosaic. Supplement: What is the principle of mosaic removal? 1. If you want to remove mosaic from a picture, it is equivalent to painting on a canvas with an outline. Although the color aspect is easier to handle, it is still very difficult to complete. because

Tutorial to quickly create desktop shortcuts in Win11 Tutorial to quickly create desktop shortcuts in Win11 Dec 27, 2023 pm 04:29 PM

In win11, we can quickly start software or files on the desktop by adding desktop shortcuts, and we only need to right-click the required files to operate. Add a desktop shortcut in win11: 1. Open "This PC" and find the file or software you want to add a desktop shortcut to. 2. After finding it, right-click to select it and click "Show more options" 3. Then select "Send to" - "Desktop Shortcut" 4. After the operation is completed, you can find the shortcut on the desktop.

How to add watermark to images in Vue? How to add watermark to images in Vue? Aug 19, 2023 pm 12:37 PM

How to add watermark to images in Vue? Vue is a popular JavaScript framework that is widely used for building web applications. Sometimes we need to add watermarks to images in Vue applications to protect the copyright of the image or increase the recognizability of the image. In this article, I will introduce you to a method of adding watermarks to images in Vue and provide corresponding code examples. The first step is to introduce a third-party library for adding watermarks to Vue. It is recommended to use watermarkj

See all articles