-1

I have this array in a string.

"['hello@test.com','holla@mail']"

I want to remove the double quotes surrounding the array.

I tried the following:

$data = "['hello@test.com','holla@mail']";

dd(json_decode($data, true));

But I am getting null.

My expected result is this:

['hello@test.com','holla@mail']

But I am getting null.

Bold
  • 213
  • 1
  • 4
  • 11
  • Your data are not json valid. You need use double quotes like @Hamatti said. If you will work with json, good advice is use online validators if your data are valid. – daremachine Aug 18 '22 at 20:27
  • https://stackoverflow.com/q/43869525/2943403 , https://stackoverflow.com/q/48573750/2943403 , https://stackoverflow.com/q/47549701/2943403 , https://stackoverflow.com/q/58250064/2943403 – mickmackusa Aug 18 '22 at 21:01
  • @mickmackusa The issue i stated here is different from the issues stated in the links you provided, the answers are just similar. Therefore, I don't think this question is a duplicate question. Also, I don't think there is a link that has the exact question I asked here. – Bold Aug 18 '22 at 21:32
  • 2
    Questions do not need to be identical. If the resolving advice on the dupe target is suitable for resolving the closed question, then the closure is appropriate. It is a major misconception that questions must be identical and this is perpetuated by the phrase "duplicate question". Notice that the big blue box at the top of this page does not say that the dupe page is an exact duplicate -- it says: "_This question already has answers here:_". When developers ask a spectrum of similar questions that are all resolved the same way, Stack Overflow wants these pages bundled together. – mickmackusa Aug 18 '22 at 21:46
  • 1
    The more important question is: Why is your input an invalid json string? Are you in control of the creation of this string? Or do you have some low-grade 3rd party tool / endpoint that is delivering this invalid json string? There will be fringe cases where your data within the string will be corrupted by executing string manipulation upon it. Rather than trying to hack at the string, I recommend going back farther a preventing this bad data from its source. – mickmackusa Aug 18 '22 at 21:48
  • Okay. I understand now – Bold Aug 19 '22 at 08:48
  • 1
    _"I want to remove the double quotes surrounding the array"_ - then your test code should probably start with a variable, that actually contains what you said it would. `$data = "['hello@test.com','holla@mail']";` obviously does not. If you _really_ had a value with double quotes at the start and end that you need to remove - then `trim($var, '"')` will do the job. – CBroe Aug 19 '22 at 08:48

2 Answers2

1

The strings in JSON need to be inside double quotes.

$data = '["hello@test.com", "holla@mail"]';
dd(json_decode($data, true));

See example #3 in json_decode documentation

Hamatti
  • 1,210
  • 10
  • 11
  • Thanks. Also, I tried to replace the single quotes with double and it works fine. ` str_replace('\'', '"', $data); dd(json_decode($data, true)); ` – Bold Aug 18 '22 at 20:30
0

convert the quotes

use Illuminate\Support\Str;

$mystr = "['hello@test.com','holla@mail']";


$out = json_decode(Str::of($mystr)->replace("'",'"'));
Snapey
  • 3,604
  • 1
  • 21
  • 19