Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove weird backslashes in printed JSON [duplicate]

I'm working on a small scraper for fun and when I grab some image urls from certain sites they come back really weird.

For example:

scraped url:

https:\/\/cdn1.vox-cdn.com\/thumbor\/zN9XawbQJgFPkuAcA2JEGgqApm8=\/cdn0.vox-cdn.com\/uploads\/chorus_asset\/file\/3700712\/tomorrowland54fdf04f23efb_2040.0.jpg

desired url:

https://cdn1.vox-cdn.com/thumbor/zN9XawbQJgFPkuAcA2JEGgqApm8=/cdn0.vox-cdn.com/uploads/chorus_asset/file/3700712/tomorrowland54fdf04f23efb_2040.0.jpg

it's adding unnecessary backslashes, so that url doesn't work when you follow it, it gives an error.

I tried using the stripslashes function as it seems like that's it's purpose but it didn't work. The url just stayed the same.

(edit) here's the code i'm using to grab urls:

function GetImages($page_dom) {
    $found_links = [];

    $images = $page_dom->getElementsByTagName('img');
    foreach ($images as $image) {
        $img_src = $image->getAttribute('src');
        $found_links[] = $img_src;
    }

    return $found_links;
}
like image 628
Scott Avatar asked Dec 06 '25 04:12

Scott


1 Answers

When you call json_encode, use the JSON_UNESCAPED_SLASHES option to prevent it from escaping slashes.

But this shouldn't really be necessary. If you're outputing JSON, you should be sending it to a program that parses JSON, and the JSON parser will translate \/ to /.

like image 135
Barmar Avatar answered Dec 07 '25 19:12

Barmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!