Question

In my string I have place holders like: ##NEWSLETTER## , ##FOOTER# ##GOOGLEANALYTICS## etc.

Each of those placeholders is delimited by: ##

I want to find each of thos placeholders and put them in an array.

The tricky part is that what's inside the ## delimiters can be anything.

Was it helpful?

Solution

Try this:

<?php

$s = "asdff ##HI## asdsad ##TEST## asdsadsadad";

preg_match_all("~##([^#]+)##~", $s, $result);

var_dump($result[1]);

prints:

array(2) {
  [0]=>
  string(2) "HI"
  [1]=>
  string(4) "TEST"
}

OTHER TIPS

you can use preg_match_all():

$str = '##NEWSLETTER## , some more text ##FOOTER## test 123 ##GOOGLEANALYTICS## aaa';
preg_match_all('/##([^#]+?)##/', $str, $matches);
var_dump($matches);

$matches[1] will have all your placeholders

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top