开发者

Finding placeholders and put then in an array

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

Each of those placehold开发者_StackOverflow社区ers 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.


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"
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜