views:

21

answers:

2

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.

A: 

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

Laimoncijus
A: 

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"
}
edorian
Please note, that if you want to be able to match a single '#' within a placeholder (eg. "##TE#ST##"), then you should change the regexp to##([^#]|#[^#])+##
Mads Ravn