简体   繁体   中英

replace json string using regex in PHP

How can i replace/remove some string in this JSON ?.I think this problem can be solve using str_replace method or preg_replace but i don't know how to add the regex

Please help me.

here my 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
)

will i use this function to export the json to 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);

the above code work fine but each image link is have one column so looks like bad , i need to make each group of pics on one column I try to add regex to part of link images except the first image url [0] and merge the other with it , that i can putting them together in one column.... so for the experiment i add this to the above code but seems nothing happen

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

here i expect the output something like that

....
[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
                        )
.....

edit this the .csv file output look like this

这个

and I'm looking to be something like that

这是

ok I've fixed by

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

now i got each group of images on one column

thanks :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM