在php中搜索## ... ##中包含的所有字符串
I'm trying to build a program that can read variables from a template and allow the user to edit them. I'm trying to use regex to find instances of user-defined variables in the template, which would look like ##VARIABLENAME##
.
I essentially want to grab the VARIABLENAME
part into an array item, for each variable that exists in the source string.
I looked around a bit but couldn't find anything, regex syntax is quite a beast to someone who hasn't used it before.
Any help appreciated!
EDIT:
Sample input:
"Hello there ##NAME##, you have ##ORDERSNUM## orders pending".
Expected output:
array[0] = 'NAME'
array[1] = 'ORDERSNUM'
I've so far tried
preg_match("/#(.*)#/", $temphtml, $tempvars)
But this is only returning the first match found, twice.
我正在尝试构建一个程序,可以从模板中读取变量并允许用户编辑它们。 我正在尝试使用正则表达式在模板中查找用户定义变量的实例,这看起来像 我本质上想要的 将 我环顾四周但找不到任何东西, 对于之前没有使用它的人来说,正则表达式语法是一种野兽。 p>
任何帮助表示赞赏! p>
编辑: p>
示例输入: p>
预期输出: p>
我到目前为止已尝试 p>
但这只返回找到的第一个匹配,两次。 p>
div> ## VARIABLENAME ## code>。 p>
VARIABLENAME code>部分抓取到数组项中,对于源字符串中存在的每个变量。 p>
“Hello there ## NAME ##,您有## ORDERSNUM ## orders pending”。
code> pre>
array [0] ='NAME'
array [1] ='ORDERSNUM'
code>
preg_match(“/#(.*)#/”,$ temphtml,$ tempvars)\ n code> pre>
Just use regular expressions like:
preg_match_all('/##([^#]+)##/', $string, $matches)
Since PHP 5.6, it's possible to do that:
$parts = explode('##', $tpl);
array_pop($parts);
$result = array_values(array_filter($parts, function($v,$k) {
return ($k&1 && !empty($v));}, ARRAY_FILTER_USE_BOTH));
You should use this RegEx:
##(.*?)##
The .*?
makes the search lazy, so after the first ##
, it will search until it finds another ##
, then stop.
To only get the data inside ##
If you look at This Demo on 3v4l, you will see that $matches[1]
has an array of the data stored in the 1st
Capture Group. Therefore, on the string:
Hello there ##NAME##, you have ##ORDERSNUM## orders pending
-
NAME
is stored in$matches[1][0]
-
ORDERSNUM
is stored in$matches[1][1]
If you use this RegEx:
##([^#]*)##
It will fail on But ##This#Will#Not#Work## will it?
, however that is not likely to be a variable name. ;)
You could use str_ireplace
class YourTemplateEngine
{
/**
* Set you html content which has all the tags inside
* @param string $content
*/
public function setContent($content)
{
$this->content = $content;
}
/**
* Set your tags and assign values
* @param string $tag
* @param sting | int |number $value
*/
public function setTagValue($tag, $value)
{
$this->tags_array[$tag] = $value;
}
/**
* Replace all tags
*/
private function replace_tags()
{
$tags = array();
$values = array();
foreach ($this->tags_array as $tag => $val) {
array_push($tags, '##'.$tag.'##');
array_push($values, $val);
}
return str_ireplace($tags, $values, $this->content);
}
/**
* Replace all tags and get output
*/
public function render()
{
return $this->replace_tags();
}
}
Then you can just to something like this.
$template = new YourTemplateEngine();
$template->setTagValue('NAME','Your Name');
$template->setTagValue('ORDERSNUM',100);
$template->setContent("Hello there ##NAME##, you have ##ORDERSNUM## orders pending");
print $template->render();
// Outputs
// Hello there Your Name, you have 100 orders pending