sir-haver
sir-haver

Reputation: 3592

PHP path string turns into '\/'

I have a mySqli table with image paths such as:

assets/images/profile_pics/00fa5fa61df8a9e22e3444c

Upon reading it with PHP like so:

<?php

require 'config/config.php';

$st=$con->prepare("SELECT * FROM products");
$st->execute();
$rs=$st->get_result();
$arr=array();
while($row=$rs->fetch_assoc()) {
    array_push($arr, $row);
}
echo json_encode($arr);

?>

The path turns into:

assets\/images\/2_pics\/00fa5fa61df8a9e22e3444c285bed8d38n.jpeg

Each / turns into /

I tried a few things such as:

$row['imageUrl'] = urldecode($row['imageUrl']);
$row['imageUrl'] = str_replace('\/', '/',$row['imageUrl']);

But it seems like / cannot be found in the string for some reason. What can I do to keep the original path without adding extra slashes?

Upvotes: 0

Views: 56

Answers (2)

Bartosz Zasada
Bartosz Zasada

Reputation: 3900

If you need to encode that array into JSON, but don't want the slashes, you should pass JSON_UNESCAPED_SLASHES flag as a second parameter to json_encode:

echo json_encode($arr, JSON_UNESCAPED_SLASHES);

Upvotes: 1

Rupesh Tiwari
Rupesh Tiwari

Reputation: 1

 $path = 'assets\/images\/2_pics\/00fa5fa61df8a9e22e3444c285bed8d38n.jpeg';

$original_path = stripslashes("assets/images/2_pics/00fa5fa61df8a9e22e3444c285bed8d38n.jpeg");

Now Your $original_path will be as your original path which is ->

assets/images/2_pics/00fa5fa61df8a9e22e3444c285bed8d38n.jpeg

Upvotes: 0

Related Questions