-1

How to properly replace \ " or remove these chars from a string? They must be taken together and not separately

$string = '{"ID":"89t9ZUIx1Mt","Title":"Pen","Attribution":"\"Pen\" by Jarlan Perez, https://poly.pizza/m/89t9ZUIx1Mt. Licence at https://creativecommons.org/licenses/by/3.0"';
$replaced = str_replace("\"\\", "",$string);
echo $replaced;

Result: {"ID":"89t9ZUIx1Mt","Title":"Pen","Attribution":"Pen\" by Jarlan Perez, https://poly.pizza/m/89t9ZUIx1Mt. Licence at https://creativecommons.org/licenses/by/3.0"

Goal:

{"ID":"89t9ZUIx1Mt","Title":"Pen","Attribution":"Pen by Jarlan Perez, https://poly.pizza/m/89t9ZUIx1Mt. Licence at https://creativecommons.org/licenses/by/3.0" 
PHP Newbie
  • 33
  • 10
  • 1
    Something like `$replaced = str_replace("\"\\", "", $string);` ... – arkascha Nov 16 '22 at 17:51
  • 4
    However, this smells a bit like an attempt to handle the symptom instead of curing the cause. Which rarely is a good idea ... Where do these escape sequences come from? Looks like data you got from some API probably. Wouldn't it make more sense to fix the data retrieval then? So that you have clean data from the beginning instead of having to try to clean the mess up later? – arkascha Nov 16 '22 at 17:52
  • This does the job done, you're 100% right, unfortunately I'm not the owner of data source, it's an external API, haven't tried contacting owner yet tbh, any other way to handle this? I'm using this data in Js, and JSON Parse fails if "\ isn't handled – PHP Newbie Nov 16 '22 at 17:56
  • Or `$replaced = str_replace(' "\\', "", $string);` and remove the leading space as well – RiggsFolly Nov 16 '22 at 17:57
  • 1
    A backslash is used for escaping the next character, so it seems not logical to replace `"\\` (double-quote followed by back-slash) with an empty string – Luuk Nov 16 '22 at 17:59
  • @PHPNewbie show the full data you're parsing from. It may be in a format that is easily parsed through some other method. – Rogue Nov 16 '22 at 18:05
  • `{"ID":"89t9ZUIx1Mt","Title":"Pen","Attribution":"\"Pen\" by Jarlan Perez, https://poly.pizza/m/89t9ZUIx1Mt. Licence at https://creativecommons.org/licenses/by/3.0"` – PHP Newbie Nov 16 '22 at 18:07
  • Data seems to be fine except "\, @arkascha For some reason your solution only removes the first \ leaving ", while the second gets unchanged – PHP Newbie Nov 16 '22 at 18:08
  • @PHPNewbie that definitely reads like a JSON object, and it should be parseable by `JSON#parse` (or the php equivalent). Edit it into your question so newlines/etc are preserved normally, the escapes as they're placed should be fine. In the final string, they'll render as just `"`. – Rogue Nov 16 '22 at 18:12
  • 1
    The value for `Attribution` should read `"Pen" by Jarlan Perez, http....` Try to replace `\"` with `#`, and then check the results. – Luuk Nov 16 '22 at 18:13
  • It should be `"Attribution":"Pen by Jarlan Perez long text"` – PHP Newbie Nov 16 '22 at 18:16
  • There is nothing wrong with this JSON. You can simply ``json_decode($jsonStr, true);`` to get values. – OMi Shah Nov 16 '22 at 18:18
  • I'd recommend using [json_decode](https://www.php.net/json_decode) instead – aynber Nov 16 '22 at 18:18
  • Read the comments please, I need to prepare data for JavaScript, Json parse gives erros if "\ are kept, tested and confirmed – PHP Newbie Nov 16 '22 at 18:19
  • Take a look at updated main post, its \" instead of "\ – PHP Newbie Nov 16 '22 at 18:24
  • Looks to me like you simply have your characters in the wrong order in the first argument to `str_replace()`. `"\"\\"` (quote first and then backslash) should be `"\\\""` (backslash first and then quote) – rickdenhaan Nov 16 '22 at 21:16

1 Answers1

0
$replaced = str_replace('\\"', '',$string);

Final solution

PHP Newbie
  • 33
  • 10
  • This should not replace anything, as there is no backslash followed by a double quote in the original string. – Luuk Nov 16 '22 at 18:57
  • Please take a look at [What does it mean to escape a string?](https://stackoverflow.com/questions/10646142/what-does-it-mean-to-escape-a-string) – Luuk Nov 16 '22 at 19:00