Fred Christian Fred Christian - 24 days ago 19
JSON Question

Regex with Day One 2 JSON Export

I'm writing a blog with Laravel 5.3 and Day One 2 app (iOS and macos) as a backend solution. Oh, and I start a world tour in two weeks, I'm a lil'bit in a hurry :/

I export my Day One 2 entries with the JSON export option, who generate a zip file which is stored on a Dropbox account. I have no problem to get the file from Dropbox, open the ZIP and get the JSON output. But...

The fact is I need an regex-guru to deal with the entrie text field, specially to find/insert image or gallery.

Here is an example of JSON output (text entry looks like Markdown syntax I guess) :

{
"metadata" : {
"version" : "1.0"
},
"entries" : [
{
"tags" : [
"discover",
"explore"
],
"uuid" : "4E032198058E43E5B740000C307F3678",
"text" : "
# The Heading Test\n
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat...\n
![](dayone-moment:\/\/6BC7C34BF2C34AFFB295014ECC818B15)\n
![](dayone-moment:\/\/E69CE22AC6AD4F38A18705499C7016ED)\n
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat...\n
\n
![](dayone-moment:\/\/74A52813A7C74897B210BCE7EFE1EB86)",
"photos" : [
{
"fnumber" : "(null)",
"orderInEntry" : 2,
"width" : 1690,
"type" : "jpeg",
"identifier" : "74A52813A7C74897B210BCE7EFE1EB86",
"height" : 1169,
"md5" : "2870b8aea3e95bdd06f9cec2d7c45a61",
"focalLength" : "(null)"
},
{
"fnumber" : "(null)",
"orderInEntry" : 1,
"width" : 2048,
"type" : "jpeg",
"identifier" : "E69CE22AC6AD4F38A18705499C7016ED",
"height" : 1184,
"md5" : "8e160d66157e3292bd1de3ced2a6c683",
"focalLength" : "(null)"
},
{
"cameraMake" : "Canon",
"fnumber" : "8",
"orderInEntry" : 0,
"width" : 2100,
"cameraModel" : "Canon EOS 1100D",
"type" : "jpeg",
"identifier" : "6BC7C34BF2C34AFFB295014ECC818B15",
"date" : "2014-03-20T18:18:05Z",
"exposureBiasValue" : 0,
"height" : 1371,
"lensModel" : "EF-S18-55mm f\/3.5-5.6 IS II",
"md5" : "9a762493484888a70e02858887654dce",
"focalLength" : "30"
}
],
"creationDate" : "2016-10-06T09:14:50Z",
"duration" : 0,
"starred" : true,
"timeZone" : "Indian\/Reunion"
}
]}


As you see, the
![](dayone-moment:\/\/photo_identifier)
'tag' contain a photo identifier, not the picture name which is not really simple. The name of jpg file is the MD5 field (yep, strange).

So the idea is:


  • remplace
    ![](dayone-moment:\/\/photo_identifier_field)
    by a
    <img
    src="photo_md5_field">

  • and if I have two or more side-by-side (or just after a \n), add a gallery like this:



<section>
<div id="wrapper">
<div><img src="photo1_md5_field"></div>
<div><img src="photo2_md5_field"></div>
<div>...<div>
</div>
</section>


Oh, one more thing... how to extract the first line (used as Title) and the second one (used as Subtitle)?

I know, it's a big request but I'm totally lost in translation with Regex :/

Thanks for your help!

Answer

Try with this code, it will do the work:

$json = json_decode($text, true);

foreach ($json['entries'] as $key => $item) {
    foreach($item['photos'] as $photo) {
        $json['entries'][$key]['text'] = preg_replace('/(\!\[\]\(dayone-moment:[\/\\\]*('.$photo['identifier'].')\).*)/', '<img src="'.$photo['md5'].'" />', $json['entries'][$key]['text']);
    }
    $json['entries'][$key]['text'] = preg_replace('/(\<img\ src=\"[a-zA-Z0-9]*\" \/\>)/', '<div>$1</div>', $json['entries'][$key]['text']);
    $json['entries'][$key]['text'] = preg_replace('/((<div\><img\ src=\"[a-zA-Z0-9]*\" \/\><\/div\>(\\n*\s*)?){2,})/', '<section><div id="wrapper">$1</div></section>', $json['entries'][$key]['text']);
}

$json = json_encode($json);
Comments