0

I want to convert following string into json. Following has a link to image then delimiter ',' then a link then a delimiter ',' then has another delimiter ',' between title and subtitle then another delimiter '#'.

$str = "http://example.com/1.jpg,http://example.com/1.html,title,subtitle#http://example.com/2.jpg,http://example.com/2.html,title,subtitle#http://example.com/3.jpg,http://example.com/3.html,title,subtitle";

I want above string to appear like following

[
    {
        image: http://example.com/1.jpg,
        link: http://example.com/1.html,
        title: title,
        subtitle: subtitle
    },
    {
        image: http://example.com/2.jpg,
        link: http://example.com/2.html,
        title: title,
        subtitle: subtitle
    },
    {
        image: http://example.com/3.jpg,
        link: http://example.com/3.html,
        title: title,
        subtitle: subtitle
    }
]

How can I achieve above in php?

2 Answers 2

3
// first split the string on the '#' delimiter
$list = explode("#", $str);

// create output array, will be used as input for json_encode function
$outputArray = array();

// go through all the lines found when string was splitted on the '#' delimiter
foreach ($list as $line)
{
    // split the single line in to four part,
    // using the ',' delimiter
    list($image, $link, $title, $subtitle) = explode(',', $line);

    // store everything in the output array
    $outputArray[] = array(
        'image' => $image,
        'link' => $link,
        'title' => $title,
        'subtitle' => $subtitle,
    );
}

// parse the array through json_encode and display the output
echo json_encode($outputArray);
Sign up to request clarification or add additional context in comments.

2 Comments

Will the last item 'subtitle' => $subtitle, with comma work or should I remove that comma?
Doesn't matter, I always place a comma at the end so I will not forget it.
2

You can do this:

$str = "http://example.com/1.jpg,http://example.com/1.html,title,subtitle#http://example.com/2.jpg,http://example.com/2.html,title,subtitle#http://example.com/3.jpg,http://example.com/3.html,title,subtitle";
$keys = Array("image", "link", "title", "subtitle");
$o = Array();
foreach(explode("#", $str) as $value) {
    $new = Array();
    foreach(explode(",", $value) as $key => $sub){
        $new[ $keys[ $key ] ] = $sub;
    }
    $o[] = $new;
}

echo json_encode($o);

Output:

 [
   {
      "image":"http:\/\/example.com\/1.jpg",
      "link":"http:\/\/example.com\/1.html",
      "title":"title",
      "subtitle":"subtitle"
   },
   {
      "image":"http:\/\/example.com\/2.jpg",
      "link":"http:\/\/example.com\/2.html",
      "title":"title",
      "subtitle":"subtitle"
   },
   {
      "image":"http:\/\/example.com\/3.jpg",
      "link":"http:\/\/example.com\/3.html",
      "title":"title",
      "subtitle":"subtitle"
   }
]

3 Comments

How would I take care of this `\ ` backslaches in html because I am going to output these without them.
The function: json_decode will remove the backslaches.
In Javascript (if you use jQuery) you can do this: $.parseJSON(data). The `` is placed by PHP to escape data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.