0

I want a regex that will look at a string like this, get the "card" value from each of these comment blocks and also, TRUE if there is a "disabled":true or "hide":true mark (false otherwise) and return an array of the matches and their status. Here's an example of a string:

<!-- wp:genesis-custom-blocks/blockname {
    "card":"card name 1",
    "boxes":"multiple boxes",
    "selected":false,
    "hide":true} /-->

more content

<!-- wp:genesis-custom-blocks/blockname {
    "card":"card name 2",
    "boxes":"multiple boxes",
    "selected":false,
    "hide":true} /-->

<div>tags, etc</div>

<!-- wp:genesis-custom-blocks/blockname2 {
    "card":"card name 4",
    "boxes":"multiple boxes",
    "selected":false,
    "hide":true} /-->

<!-- wp:genesis-custom-blocks/blockname10 {
    "selected":false} /-->

<!-- wp:genesis-custom-blocks/blockname2 {
    "card":"card name 6",
    "selected":false} /-->

other content

<!-- wp:genesis-custom-blocks/blockname2 {
    "card":"card name 5",
    "boxes":"multiple boxes",
    "selected":false,
    "disabled":true} /--> 

<!-- wp:genesis-custom-blocks/blockname2 {
    "card":"card name 5",
    "boxes":"multiple boxes",
    "selected":false} /--> 

so for example for the string above I'd want:

[
    ["card name 1", true],
    ["card name 2", true],
    ["card name 4", true],
    ["card name 6", false],
    ["card name 5", true],
    ["card name 5", false]
]

I used:

preg_match_all('/"card":"(.*?)"/m', $content, $matches);

Which gets the cards values, but not the other information. What's the best way to do this? I know it will use some version of [hide|disabled]. "card" will always be the first value in these blocks, if that makes a difference.

1
  • 1
    Everything after wp:genesis-custom-blocks/blockname looks like JSON. So I suggest you extract the comment block contents, remove that word from the beginning, and use json_decode() to parse it. Commented Apr 11, 2024 at 23:32

1 Answer 1

2

Use a regular expression to match the comment block, and get the JSON from inside it. Then you can use json_decode() to parse it.

preg_match_all('#<!-- wp:genesis-custom-blocks/blockname\d* (.*?) /-->#s', $content, $matches);
$results = array_map(fn ($json) => json_decode($json, true), $matches[1]);
var_dump($results);

output:

array(7) {
  [0]=>
  array(4) {
    ["card"]=>
    string(11) "card name 1"
    ["boxes"]=>
    string(14) "multiple boxes"
    ["selected"]=>
    bool(false)
    ["hide"]=>
    bool(true)
  }
  [1]=>
  array(4) {
    ["card"]=>
    string(11) "card name 2"
    ["boxes"]=>
    string(14) "multiple boxes"
    ["selected"]=>
    bool(false)
    ["hide"]=>
    bool(true)
  }
  [2]=>
  array(4) {
    ["card"]=>
    string(11) "card name 4"
    ["boxes"]=>
    string(14) "multiple boxes"
    ["selected"]=>
    bool(false)
    ["hide"]=>
    bool(true)
  }
  [3]=>
  array(1) {
    ["selected"]=>
    bool(false)
  }
  [4]=>
  array(2) {
    ["card"]=>
    string(11) "card name 6"
    ["selected"]=>
    bool(false)
  }
  [5]=>
  array(4) {
    ["card"]=>
    string(11) "card name 5"
    ["boxes"]=>
    string(14) "multiple boxes"
    ["selected"]=>
    bool(false)
    ["disabled"]=>
    bool(true)
  }
  [6]=>
  array(3) {
    ["card"]=>
    string(11) "card name 5"
    ["boxes"]=>
    string(14) "multiple boxes"
    ["selected"]=>
    bool(false)
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Oooh this is nice!
I get the "unexpected '=>' (T_DOUBLE_ARROW), expecting ',' or ')'" on the 2nd line though.
It's just my version of PHP though. Thanks!

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.