1

let's say i have

$flags=JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;

how can i then remove JSON_UNESCAPED_SLASHES from $flags ?

hanshenrik
  • 19,904
  • 4
  • 43
  • 89
  • Yes, that's exactly the way to do it. – Nick Sep 04 '19 at 11:56
  • Rather than posting your own answer in the question body, split your question into both a question and own answer. If there are better alternatives around, those can then still be added and upvoted by others. – k0pernikus Sep 04 '19 at 11:57
  • When writing a question, there is even a checkbox "Add your own answer" – k0pernikus Sep 04 '19 at 11:58
  • See also [operators.bitwise](https://www.php.net/manual/en/language.operators.bitwise.php) – Adder Sep 04 '19 at 11:59
  • What's the point of this bitwise OR? What does `$flags` do? – nice_dev Sep 04 '19 at 12:02
  • 1
    @vivek_23 you can find the documentation for all of those flags at https://www.php.net/manual/en/json.constants.php - `JSON_PRETTY_PRINT` tries to make the output look "pretty", JSON_UNESCAPED_SLASHES makes it not escape slashes (by default `/` is escaped as `\/`, which is not required by JSON, but php does it anyway, which can optionally be turned off), `JSON_UNESCAPED_UNICODE` makes it add unicode characters in strings literally instead of adding a unicode escape character looking like \uXXXX) – hanshenrik Sep 04 '19 at 12:06
  • `$flags` contains several bits of information in one number. – Adder Sep 04 '19 at 12:06

3 Answers3

5

It's not "C method" it's just applying bitwise operators

$flags=JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;

var_dump($flags & JSON_UNESCAPED_SLASHES); // flag should be set to 1

$flags &= ~JSON_UNESCAPED_SLASHES; // remove it  

var_dump($flags & JSON_UNESCAPED_SLASHES); // flag should be set to 0
Robert
  • 19,800
  • 5
  • 55
  • 85
3

using the C method

$flags &= ~JSON_UNESCAPED_SLASHES;

seems to work.

hanshenrik
  • 19,904
  • 4
  • 43
  • 89
0

In case you can't reach flags directly, as their are added later in 3rd party code (my case):

<?php

function notMyFunction($flags)
{
    $finalFlags = $flags | FLAG_I_DONT_WANT;
    // ...
}

There is this trick:

<?php


notMyFunction(FLAG_I_DONT_WANT - 1);
Tomas Votruba
  • 23,240
  • 9
  • 79
  • 115