PHP 中的扩展addslashes 函数

发布于 2024-11-27 05:30:15 字数 1049 浏览 3 评论 0原文

您好,有人可以帮助我构建一个扩展的addslashes 函数,该函数将与对象和数组的混合组合一起使用。例如我有这个对象:

  $object = new stdClass;
  $object2 = new stdClass;
  $object2->one = "st'r2";
  $object3 = new stdClass;
  $object3->one = "st'r3";
  $object->one = "s'tr";
  $object->two = array($object2);
  $object->obj = $object3;

我想得到这个对象转义并具有相同的结构。 我已经开始了一些实验,我得到了这样的结果:

function addslashes_extended($arr_r){
            if(is_array($arr_r)){
                foreach ($arr_r as $key => $val){
                    is_array($val) ? addslashes_extended($val):$arr_r[$key]=addslashes($val);
                }
            unset($val);
            }else if(is_object($arr_r)){
                $objectProperties = get_object_vars($arr_r);
                foreach($objectProperties as $key => $value){
                    is_object($value) ? addslashes_extended($value):$arr_r->{$key}=addslashes($value);
                }
            }
        return $arr_r;
}

但这是行不通的,我认为我必须通过引用传递,但我不知道如何,其他解决方案也很好,提前感谢!

Hi can somebody help me with building an extended addslashes function, which will work with mixed combination of objects and arrays. For example i have this Object:

  $object = new stdClass;
  $object2 = new stdClass;
  $object2->one = "st'r2";
  $object3 = new stdClass;
  $object3->one = "st'r3";
  $object->one = "s'tr";
  $object->two = array($object2);
  $object->obj = $object3;

And i would like to get this object back escaped and with the same structure.
I have started some experiments and i get something like this:

function addslashes_extended($arr_r){
            if(is_array($arr_r)){
                foreach ($arr_r as $key => $val){
                    is_array($val) ? addslashes_extended($val):$arr_r[$key]=addslashes($val);
                }
            unset($val);
            }else if(is_object($arr_r)){
                $objectProperties = get_object_vars($arr_r);
                foreach($objectProperties as $key => $value){
                    is_object($value) ? addslashes_extended($value):$arr_r->{$key}=addslashes($value);
                }
            }
        return $arr_r;
}

But this is not going to work, i have to work with passing by reference i think, but i have no clue how, other solutions would be nice to have too, thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦亿 2024-12-04 05:30:15

试试这个(使用 array_walk):

error_reporting(E_ALL ^ E_STRICT);
ini_set('display_errors', 'on');

$data = array(
    "fo'o",
    'bar' => "foo'bar",
    'foobar' => array(
        1, 2, 'someObj' => json_decode('{"prop1": "a", "prop2": "b\'c"}')
    )
);

class Util
{
    public static function addslashes_extended(&$mixed) {
        if (is_array($mixed) || is_object($mixed)) {
            array_walk($mixed, 'Util::addslashes_extended');
        }
        elseif (is_string($mixed)) {
            $mixed = addslashes($mixed);
        }
    }

}

Util::addslashes_extended($data);

print_r($data);

输出 ( < a href="http://codepad.org/nUUYKWrn" rel="nofollow">http://codepad.org/nUUYKWrn ):

Array
(
    [0] => fo\'o
    [bar] => foo\'bar
    [foobar] => Array
        (
            [0] => 1
            [1] => 2
            [someObj] => stdClass Object
                (
                    [prop1] => a
                    [prop2] => b\'c
                )

        )

)

Try this (using array_walk):

error_reporting(E_ALL ^ E_STRICT);
ini_set('display_errors', 'on');

$data = array(
    "fo'o",
    'bar' => "foo'bar",
    'foobar' => array(
        1, 2, 'someObj' => json_decode('{"prop1": "a", "prop2": "b\'c"}')
    )
);

class Util
{
    public static function addslashes_extended(&$mixed) {
        if (is_array($mixed) || is_object($mixed)) {
            array_walk($mixed, 'Util::addslashes_extended');
        }
        elseif (is_string($mixed)) {
            $mixed = addslashes($mixed);
        }
    }

}

Util::addslashes_extended($data);

print_r($data);

Output ( http://codepad.org/nUUYKWrn ):

Array
(
    [0] => fo\'o
    [bar] => foo\'bar
    [foobar] => Array
        (
            [0] => 1
            [1] => 2
            [someObj] => stdClass Object
                (
                    [prop1] => a
                    [prop2] => b\'c
                )

        )

)
随心而道 2024-12-04 05:30:15

要通过引用传递,请在变量名称前使用 &,简单示例:

function inc(&$var) {
    $var++;
}

$x = 5;
inc($x);
echo $x; //Prints: 6

To pass by reference, you use & before the variable name, quick example:

function inc(&$var) {
    $var++;
}

$x = 5;
inc($x);
echo $x; //Prints: 6
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文