-4

我有一个这样的数组:

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);

...不取代'in "I'm In Like With You"

4

5 回答 5

2

假设您想递归地转义所有数组项......您的代码无法工作;str_replace()仅在字符串中替换。您可以使用array_walk()来有效地做到这一点:

在 php 5.3 及更高版本中(支持匿名函数):

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

或使用foreach循环

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

但是,我仍然觉得您需要它来转义 MySQL 查询的数据,您应该使用类似mysqli_real_escape_string,的函数mysql_real_escape_stringPDO::quote()或者只是在准备好的语句中使用占位符

于 2013-04-15T17:23:41.187 回答
1

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

例如这样的:

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

阅读更多关于“Foreach”的信息here

于 2013-04-15T17:23:33.893 回答
0

您不能直接在数组中替换。您可以将数组用作“查找此内容”和“替换为”文本,但您正在操作的内容必须是纯字符串。

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

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

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

  $array[$key]['name'] = str_replace(....,...., $element);
于 2013-04-15T17:21:58.117 回答
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;
}
于 2013-04-15T17:25:54.790 回答
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功能您只需更改第二个参数

于 2013-04-15T17:27:02.963 回答