search
Homephp教程PHP源码封装防sql注入的方法

php代码

<?php


/**
 * 批量实体转义
 * @param $data
 * @return array|string
 */
function deepSpecialChars($data)
{
    if (empty($data)) {
        return $data;
    }

    return is_array($data) ? array_map("deepSpecialChars", $data) : htmlspecialchars($data);

}

/**
 *批量单引号转义
 * @param $data
 * @return array|string
 */
function deepSlashes($data)
{
    if (empty($data)) {
        return $data;
    }
    return is_array($data) ? array_map(&#39;deepSlashes&#39;, $data) : addslashes($data);
}


//调用案例
$arr = array(&#39;username&#39; => &#39;张三<p></p>&#39;, &#39;age&#39; => "18&#39;#", &#39;desc&#39; => &#39;<script>alert("hello")</script>&#39;);


$arr = deepSpecialChars($arr);//标签转义成实体
$arr = deepSlashes($arr);//单引号转义

print_r($arr);


//result
/*
Array
(
    [username] => 张三&lt;p&gt;&lt;/p&gt;
    [age] => 18\&#39;#
    [desc] => &lt;script&gt;alert(&quot;hello&quot;)&lt;/script&gt;
)
*/
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use