str_replace()不适用于数组

时间:2013-04-15 17:13:13

标签: php mysql sql

我有一个这样的数组:

array(46) 
{ 
[0]=> array(4) { ["category"]=> string(21) "Media/news/publishing" ["name"]=> string(20) "Cosmopolitan Armenia" ["id"]=> string(15) "146307748762264" ["created_time"]=> string(24) "2013-03-20T14:40:43+0000" } 
[1]=> array(4) { ["category"]=> string(17) "Telecommunication" ["name"]=> string(5) "Zangi" ["id"]=> string(15) "386291674718829" ["created_time"]=> string(24) "2013-03-20T11:07:46+0000" } 
[2]=> array(4) { ["category"]=> string(13) "Musician/band" ["name"]=> string(11) "Ray Charles" ["id"]=> string(12) "430894850091" ["created_time"]=> string(24) "2013-03-16T22:03:46+0000" } [3]=> array(4) { ["category"]=> string(4) "Cars" ["name"]=> string(13) "I'm In Like With You" ["id"]=> string(15) "447042531989769" ["created_time"]=> string(24) "2013-03-16T22:01:46+0000" } .......
}

有没有人知道为什么这段代码:

str_replace("'","\'",MY_ARRAY);

...不会取代'中的"I'm In Like With You"

5 个答案:

答案 0 :(得分:2)

假设您想以递归方式转义所有数组项...您的代码无法正常工作; str_replace()仅在字符串中替换。您可以使用array_walk()来有效地执行此操作:

在php 5.3及更高版本中(支持anonymous functions):

array_walk($data, function(&$item, $key){
    array_walk( $item, function(&$item, $key){
        $item = str_replace("'","\'", $item);
    });
});

或使用foreach loop

foreach( $data as &$v1){
    foreach( $v1 as &$v2){
        $v2 = str_replace("'","\'", $v2);
    }
}

但是,我仍然觉得你需要这个来逃避MySQL查询的数据,你应该使用像mysqli_real_escape_stringmysql_real_escape_stringPDO::quote()这样的函数或者只是{{3} }。

答案 1 :(得分:1)

使用str_replace,您需要遍历数组并对每个值执行str_replace

例如:

foreach( $yourArray as $key => $value)
{
    $yourArray[ $key ]['name'] = str_replace("'", "\'", $value);
}

详细了解“Foreach”here

答案 2 :(得分:0)

您不能直接在数组中替换。你可以使用数组作为“找到这个”和“替换为”文本,但你操作的必须是一个普通的字符串。

你必须在数组上循环,然后对每个字符串执行replace操作:

foreach($array as $key => $element) {
    $array[$key] = str_replace(..., ..., $element);
}

鉴于您的数组中有多个单独的字符串,您必须处理每个字符串:

  $array[$key]['name'] = str_replace(....,...., $element);

答案 3 :(得分:0)

你可以试试这个简单的递归函数

示例

$object = new stdClass();
$object->game = "GOW'4";
$object->whoami = "Peehaa'";

$array = array(
        "nice'kkk",
        "Fish" => array(
                "X" => "one'one",
                "obj" => $object
        )
);

print_r(str_replace_recursive("'", "*", $array));

输出

Array
(
    [0] => nice*kkk
    [Fish] => Array
        (
            [X] => one*one
            [obj] => stdClass Object
                (
                    [game] => GOW*4
                    [whoami] => Peehaa*
                )

        )

)

使用的功能

function str_replace_recursive($search, $replace, $subject) {
    $result = null;
    if (is_array($subject)) {
        $result = array();
        foreach ( $subject as $k => $var ) {
            $result[$k] = str_replace_recursive($search, $replace, $var);
        }
    } elseif (is_object($subject)) {
        $result = clone $subject;
        foreach ( $subject as $k => $var ) {
            $result->{$k} = str_replace_recursive($search, $replace, $var);
        }
    } else {
        $result = str_replace($search, $replace, $subject);
    }

    return $result;
}

答案 4 :(得分:0)

您可以在array内循环,然后使用$data,但需要

$array = array(array('category' => 'Media/news/publishing', 
           'name' => 'Cosmopolitan Armenia' ,
           'id' => '146307748762264',
           'created_time' => '2013-03-20T14:40:43+0000'),
 array( 
           'category' => 'Telecommunication',
           'name' => 'Zangi',
           'id' => '386291674718829',
           'created_time' => '2013-03-20T11:07:46+0000'), 
 array(  
           'category' => 'Musician/band',
           'name' => 'Ray Charles',
           'id' => '430894850091',
           'created_time' => '2013-03-16T22:03:46+0000'),
 array( 
           'category' => 'Cars' ,
           'name' => 'I'm In Like With You',
           'id' => '447042531989769',
           'created_time' => '2013-03-16T22:01:46+0000')
);

foreach($array as $arr)
{
    $data = str_replace('\'', '\\\'', $arr);
    echo $data['name'] . '<br>';
}

这将输出

Cosmopolitan Armenia
Zangi
Ray Charles
I\'m In Like With You

您可以根据需要更改str_replace功能,只需更改第二个参数

即可