Replace data in array based on regex

Hey everyone,

I have the next array with data (which is dynamically generated).
Now I want to do some Magic and tweak the array :star2:.

array(1) {
  ["table"]=>
  array(3) {
    ["header"]=>
    array(4) {
      [0]=>
      array(1) {
        ["c"]=>
        string(4) "Naam"
      }
      [1]=>
      array(1) {
        ["c"]=>
        string(7) "Functie"
      }
      [2]=>
      array(1) {
        ["c"]=>
        string(13) "Nevenfuncties"
      }
      [3]=>
      array(1) {
        ["c"]=>
        string(34) "Aandachtsgebieden en/of commissies"
      }
    }
    ["caption"]=>
    bool(false)
    ["body"]=>
    array(3) {
      [0]=>
      array(4) {
        [0]=>
        array(1) {
          ["c"]=>
          string(16) "*|class:orange|*"
        }
        [1]=>
        array(1) {
          ["c"]=>
          string(6) "dsasad"
        }
        [2]=>
        array(1) {
          ["c"]=>
          string(0) ""
        }
        [3]=>
        array(1) {
          ["c"]=>
          string(0) ""
        }
      }
      [1]=>
      array(4) {
        [0]=>
        array(1) {
          ["c"]=>
          string(4) "brrr"
        }
        [1]=>
        array(1) {
          ["c"]=>
          string(6) "adsdsa"
        }
        [2]=>
        array(1) {
          ["c"]=>
          string(0) ""
        }
        [3]=>
        array(1) {
          ["c"]=>
          string(0) ""
        }
      }
      [2]=>
      array(4) {
        [0]=>
        array(1) {
          ["c"]=>
          string(6) "dsasad"
        }
        [1]=>
        array(1) {
          ["c"]=>
          string(6) "dsadas"
        }
        [2]=>
        array(1) {
          ["c"]=>
          string(4) "dsad"
        }
        [3]=>
        array(1) {
          ["c"]=>
          string(0) ""
        }
      }
    }
  }
}

When we look at the [‘header’] it contains [‘c’] (the cell data). This can be text, but also a tag.
For example: *|class:orange|* here some text.

Now I want to split those up and overwrite the [‘c’] if it contains ‘|class:orange|’.
So when you have this:

   array(1) {
        ["c"]=>
        string(7) "*|class:orange|* hello"
      }

It would become this:

   array(2) {
        ["c"]=>
        string(7) "hello",
        ["class"]=>
        string(7) "orange",
      }

This way I could split the class and add it to the array. But I am stuck at the preg_match.

foreach ($table as &$row) {

    foreach ($row['header'] as &$header) {
//        $header['class'] = 123;

        preg_match('/\*\|class:([^\|])\|\*/', $header['c'], $matches);

    }
}

I need to do 2 things

  • Add an attribute to the array ($header[‘class’]) with the class after class:example.
  • I need to replace the $header[‘c’] so it does not contain *|class:orange|* and only the rest of the text.

How can I do this :slight_smile:, besides that, I think that my regex is incorrect (and I think I also need to use a preg_replace).

i would go for named groups

\*\|class:(?<class>.*)\|\* (?<text>.*)

and then just set the keys accordingly.

I mean…i’d think it’d be easier to handle it during output rather than trying to manipulate the array directly.

Also the regex to match this rather depends on the definition.

Does the *|class:moo|* always come first? Will there always only ever be one tag?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.