Home > Software engineering >  How to replace url http:/\/\example.com/\/\home using preg_replace in php
How to replace url http:/\/\example.com/\/\home using preg_replace in php

Time:10-24

This JSON RESULT

{
"status": false,
"data": {
    "pesan": "https:\/\/api.sandbox.veritrans.co.id\/v2\/qris\/8d1c5f8d-99c2-4995-ba7b-cd95b6215e85\/qr-code"
}
}

i want to replace \/\/ with /

Like this

https://api.sandbox.veritrans.co.id/v2/qris/c535e049-b09e-44fc-b901-4ec840c95106/qr-code

CodePudding user response:

I guess you want to replace \/ by /

/ is escaped while JSON encoding.

To remove escape character you just need to decode JSON :

$json = <<<JSON
    {
        "status": false,
        "data": {
            "pesan": "https:\/\/api.sandbox.veritrans.co.id\/v2\/qris\/8d1c5f8d-99c2-4995-ba7b-cd95b6215e85\/qr-code"
        }
    }
    JSON;

// Decoding in object
$result = json_decode($json);
print_r($result);
echo "The URL is : {$result->data->pesan}\r\n";

// Decoding in associative array
$result = json_decode($json, true);
print_r($result);
echo "The URL is : {$result['data']['pesan']}\r\n";

Decoding in object gives :

stdClass Object
(
    [status] => 
    [data] => stdClass Object
        (
            [pesan] => https://api.sandbox.veritrans.co.id/v2/qris/8d1c5f8d-99c2-4995-ba7b-cd95b6215e85/qr-code
        )

)
  • Related