2

I have this data:

{"names":["George","Eric","Alice"]}

And I want to use preg_match_all to filter out the words in between quotation like this:

$s = $data;

if (preg_match_all('/"([^:]*)"/', $s, $matches)) {
        echo join("\n", $matches[1]);
}

But this is outputting names George","Eric","Alice I tried many things but I cant figure it out.

9
  • 1
    Why not use a JSON parser? Commented Jan 30, 2014 at 14:08
  • 1
    json_decode() to the rescue print_r(json_decode('{"names":["George","Eric","Alice"]}', true)); ! Commented Jan 30, 2014 at 14:08
  • Its JSON but Im using it as plain text in my project Commented Jan 30, 2014 at 14:12
  • @Youss It's very clear that this is just to show what the array will contain. You only have to call the elements of this array or just use join(). Commented Jan 30, 2014 at 14:14
  • 1
    @Youss ok, you haven't mentioned that beforehand. But you could use a foreach($arr, $key => $value){} loop, it actually depends on how random your source is. Anyways, parsing JSON with regex isn't "recommended" it's still possible with regex though Commented Jan 30, 2014 at 14:25

4 Answers 4

3

* matches greedy (as much as possible). Use non-greey version: *?

if (preg_match_all('/"([^:]*?)"/', $s, $matches)) {
    echo join("\n", $matches[1]);
}

output:

names
George
Eric
Alice

UPDATE

json_decode is more appropriate for this kind of work. Try following:

foreach (json_decode($s, true) as $key => $value) {
    echo $key . "\n";
    echo join("\n", $value);
}
Sign up to request clarification or add additional context in comments.

7 Comments

Don't treat json data like strings! Bad practice.
@Roebie, I updated the answer. I just wanted to tell the reason why OP get the result.
@falsetru: Sorry, our comments crossed.
You have the variable 'names' This is the problem, Im scraping the JSON and I dont know the variable upfront, the JSON is not mine and the data keeps changing
Actually you need a complexer regex to take into account escaped double quotes (?<!\\)"((?:[^\\]|\\.)*?)"
|
2

This is actually JSON string use json_decode to parse it rather than using regex on this:

print_r(json_decode('{"names":["George","Eric","Alice"]}', true));

OUTPUT:

Array
(
    [names] => Array
        (
            [0] => George
            [1] => Eric
            [2] => Alice
        )

)

2 Comments

The thing is Im outputting this as plain text not as json. I really just need the words not the quotation, brackets and so on. (my project requires it to be plain text..)
Then json_decode can be used as: print_r(json_decode($data, true)); where $data is your variable containing this plain text. That is much cleaner and safer than regex.
1

try this

$strContent = '{"names":["George","Eric","Alice"]}';
$strRegex = '%\"(.+?)\"%s';
if (preg_match_all($strRegex, $strContent, $arrMatches))
{
    var_dump($arrMatches[1]);
}

Comments

1

Since your data is json formatted you should really treat is as json and not process it with regex which is to be used for strings. Try this:

$json = '{"names":["George","Eric","Alice"]}';
$data = json_decode($json, true);
foreach($data['names'] as $item) echo "$item\n";

Or without the hard coded "names":

$json = '{"names":["George","Eric","Alice"]}';
$data = json_decode($json, true);
foreach($data as $arr) foreach($arr as $item) echo "$item\n";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.