Finding placeholders and put then in an array

早过忘川 提交于 2019-12-08 06:03:48

问题


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.


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/2388083/finding-placeholders-and-put-then-in-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!