1

code:

$hyperlink = str_replace("%2F","","http://localhost/android/images/");
if($num>0)
{
    $products_arr=array();
    $products_arr["data"]=array();
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
    {
        extract($row);
        $product_item=array(
            "image_name" => $image_name,
            "image_url" => $hyperlink.$file_s
        );

        array_push($products_arr["data"], $product_item);
    }

    echo json_encode($products_arr);
}

I have create a json file using php now I want to add hyperlink inside an array function. I am using str_replace("%2F","","http://localhost/android/images/"); but its not working and json file look like as mention below:

{
  "data": [
    {
      "image_name": "cap-1",
      "image_url": "http:\/\/localhost\/android\/images\/Cap-01.png"
    },
    {
      "image_name": "cap-2",
      "image_url": "http:\/\/localhost\/android\/images\/Cap-02.png"
    },
    {
      "image_name": "cap-3",
      "image_url": "http:\/\/localhost\/android\/images\/Cap-03.png"
    },
    {
      "image_name": "ear-1",
      "image_url": "http:\/\/localhost\/android\/images\/Ears-01.png"
    },
    {
      "image_name": "ear-2",
      "image_url": "http:\/\/localhost\/android\/images\/Ears-02.png"
    },
    {
      "image_name": "ear-3",
      "image_url": "http:\/\/localhost\/android\/images\/Ears-03.png"
    },
    {
      "image_name": "hair-1",
      "image_url": "http:\/\/localhost\/android\/images\/Hair Color-01.png"
    },
    {
      "image_name": "hair-2",
      "image_url": "http:\/\/localhost\/android\/images\/Hair Color-02.png"
    }
  ]
}

So how can I remove "\" element from url? Please help me.

Thank You

omkara
  • 974
  • 5
  • 24
  • 50
  • try `str_replace("\\/", "/", $url);` or try using `JSON_UNESCAPED_SLASHES` option when calling `json_encode` let me know if either of those help – Isaac Aug 18 '18 at 11:46
  • not working @Isaac – omkara Aug 18 '18 at 11:49
  • can you post your attempt of trying to implement either of them? – Isaac Aug 18 '18 at 11:52
  • what is the use of JSON_UNESCAPED_SLASHES @Isaac – omkara Aug 18 '18 at 11:52
  • Take a look at this thread here, it might be of some help https://stackoverflow.com/questions/1580647/json-why-are-forward-slashes-escaped. an example of this is `$example = json_encode($array, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);` it;s an option when calling `json_encode` function – Isaac Aug 18 '18 at 11:53
  • OMG @Isaac I am using ` echo str_replace('', '<\/', json_encode($obj, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));` an d it works perfectly. Thanks buddy. – omkara Aug 18 '18 at 11:59
  • No problem! glad I could help – Isaac Aug 18 '18 at 12:00

1 Answers1

0

Calling the option JSON_UNESCAPED_SLASHES within json_encode will fix your issue.

$example = json_encode($array, JSON_UNESCAPED_UNICODE | 
JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
Isaac
  • 784
  • 10
  • 23