【问题标题】:replace json string using regex in PHP在 PHP 中使用正则表达式替换 json 字符串
【发布时间】:2016-03-29 07:28:54
【问题描述】:

如何替换/删除此 JSON 中的某些字符串?我认为可以使用 str_replace 方法或 preg_replace 解决此问题 但我不知道如何添加正则表达式

请帮帮我。

这是我的json

Array
(
    [data] => Array
        (
            [0] => Array
                (
                    [DESC] => bla bal bal
                    [SOLD] => 0
                    [contact_no] => 1234
                    [title] =>  Hiiiii
                    [price] => 10900
                    [big_image] => Array
                        (
                            [0] => http://example.com/images/user_adv/14.jpg
                            [1] => http://example.com/images/user_adv/15.jpg
                        )

                    [small_image] => Array
                        (
                            [0] => http://example.com/images/user_adv/small/14.jpg
                            [1] => http://example.com/images/user_adv/small/15.jpg
                        )

                [tpe] => user
            )

            [1] => Array
                (
                    [DESC] => fo fo fof ofof
                    [SOLD] => 0
                    [contact_no] => 234522
                    [title] => Hellooooo sddf
                    [price] => 0
                    [big_image] => Array
                        (
                            [0] => http://example.com/images/user_adv/154.jpg
                            [1] => http://example.com/images/user_adv/144.jpg
                            [2] => http://example.com/images/user_adv/147.jpg
                        )

                     [small_image] => Array
                        (
                            [0] => http://example.com/images/user_adv/small/154.jpg
                            [1] => http://example.com/images/user_adv/small/144.jpg
                            [2] => http://example.com/images/user_adv/small/147.jpg
                        )

                    [tpe] => user
                )

        )

    [pis] => 3
    [totals] => 23
    [curpage] => 1
    [total_ads] => 71
)

我会使用这个函数将 json 导出到 csv

function array_flatten ($nonFlat) {
    $flat = array();
    foreach (new RecursiveIteratorIterator(
            new RecursiveArrayIterator($nonFlat)) as $k=>$v) {
        $flat[$k] = $v;
    }
    return $flat;
}

$fp = fopen("output.csv","w");
foreach ($json['data'] as $fields) {
    fputcsv($fp, array_flatten($fields));
}
fclose($fp);

上面的代码工作正常,但每个图片链接都有一列,所以看起来很糟糕,我需要将每组图片放在一列 我尝试将正则表达式添加到链接图像的一部分,除了第一个图像 url [0] 并将另一个与它合并,我可以将它们放在一个列中...... 所以对于实验,我将它添加到上面的代码中,但似乎什么也没发生

 $flat[$k] = str_replace('[1-7] => http', "http", $v); 

我希望输出类似这样的输出

....
[big_image] => Array
                        (
                            [0] => http://example.com/images/user_adv/154.jpg
                            http://example.com/images/user_adv/144.jpg
                            http://example.com/images/user_adv/147.jpg
                        )

                    [small_image] => Array
                        (
                            [0] => http://example.com/images/user_adv/small/154.jpg
                            http://example.com/images/user_adv/small/144.jpg
                            http://example.com/images/user_adv/small/147.jpg
                        )
.....

编辑这个.csv文件输出看起来像这样

我希望成为那样的人

【问题讨论】:

  • $flat[$k] = $v;改成$flat[$k] = is_array($v)?implode(' ',$v):$v;,如果我没听错的话
  • 我已经添加但确实做了任何更改
  • 我想我没有得到 will i use this function to export the json to csv ,因为你确实打开和 csv 并在你的代码中转换它?
  • 可能会添加意外的输出
  • 我已经编辑了我的问题并添加了 csv 文件输出和期望的屏幕截图,顺便说一句,我没有放完整的代码,但是我从在线资源获得的 json 数据然后解码并转换它到 csv。

标签: php json regex


【解决方案1】:

好的,我已经修复了

foreach (
    new RecursiveArrayIterator($nonFlat) as $k=>$v) {
 $flat[$k] = is_array($v)?implode(" ",$v):$v;

现在我将每组图像放在一个列上

谢谢:)

【讨论】:

    猜你喜欢
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    • 2015-02-27
    相关资源
    最近更新 更多