PHP preg_replace 多次
PHP preg_replace multiple times
我想制作一个(模板)系统,所以我需要为一个值替换标签。该模板存储在名为 'template.tpl' 的文件中,包含以下内容:
{title}
{description}
{userlist}
{userid} is the id of {username}
{/userlist}
我有以下 PHP 脚本来重写标签:
$template = file_get_contents('template.tpl');
$template = preg_replace('/{title}/', 'The big user list', $template);
$template = preg_replace('/{description}/', 'The big storage of all the users', $template);
现在我想扩展脚本以便重写 {userlist}。我有以下数据数组:
$array = array(
1 => "Hendriks",
2 => "Peter"
);
如何创建一个 returns 例如以下输出的脚本?
The big user list
The big storage of all the users
1 is the id of Hendriks
2 is the id of Peter
我希望我已经解释得尽可能清楚了。
这是一个开始...
此代码背后的想法是找到每个 {tag}{/tag} 之间的内容并通过函数将其发回,这也允许嵌套的 foreach 迭代,但没有太多检查,例如区分大小写是一个问题,它不会清除不匹配的标签。那是你的工作:)
$data = array();
$data['title'] = 'The Title';
$data['description'] = 'The Description';
$data['userlist'] = array(
array('userid'=>1,'username'=>'Hendriks'),
array('userid'=>2,'username'=>'Peter"')
);
$template = '{title}
{description}
{userlist}
{userid} is the id of {username} {title}
{/userlist}';
echo parse_template($template,$data);
function parse_template($template,$data)
{
// Foreach Tags (note back reference)
if(preg_match_all('%\{([a-z0-9-_]*)\}(.*?)\{/\}%si',$template,$matches,PREG_SET_ORDER))
{
foreach( $matches as $match )
{
if(isset($data[$match[1]]) and is_array($data[$match[1]]) === true)
{
$replacements = array();
foreach( $data[$match[1]] as $iteration )
{
$replacements[] = parse_template($match[2],$iteration);
//$replacements[] = parse_template($match[2],array_merge($data,$iteration)); // You can choose this behavior
}
$template = str_replace($match[0],implode(PHP_EOL,$replacements),$template);
}
}
}
// Individual Tags
if(preg_match_all('/\{([a-z0-9-_]*)\}/i',$template,$matches,PREG_SET_ORDER))
{
foreach( $matches as $match )
{
if(isset($data[$match[1]]))
{
$template = str_replace($match[0],$data[$match[1]],$template);
}
}
}
return $template;
}
我想制作一个(模板)系统,所以我需要为一个值替换标签。该模板存储在名为 'template.tpl' 的文件中,包含以下内容:
{title}
{description}
{userlist}
{userid} is the id of {username}
{/userlist}
我有以下 PHP 脚本来重写标签:
$template = file_get_contents('template.tpl');
$template = preg_replace('/{title}/', 'The big user list', $template);
$template = preg_replace('/{description}/', 'The big storage of all the users', $template);
现在我想扩展脚本以便重写 {userlist}。我有以下数据数组:
$array = array(
1 => "Hendriks",
2 => "Peter"
);
如何创建一个 returns 例如以下输出的脚本?
The big user list
The big storage of all the users
1 is the id of Hendriks
2 is the id of Peter
我希望我已经解释得尽可能清楚了。
这是一个开始...
此代码背后的想法是找到每个 {tag}{/tag} 之间的内容并通过函数将其发回,这也允许嵌套的 foreach 迭代,但没有太多检查,例如区分大小写是一个问题,它不会清除不匹配的标签。那是你的工作:)
$data = array();
$data['title'] = 'The Title';
$data['description'] = 'The Description';
$data['userlist'] = array(
array('userid'=>1,'username'=>'Hendriks'),
array('userid'=>2,'username'=>'Peter"')
);
$template = '{title}
{description}
{userlist}
{userid} is the id of {username} {title}
{/userlist}';
echo parse_template($template,$data);
function parse_template($template,$data)
{
// Foreach Tags (note back reference)
if(preg_match_all('%\{([a-z0-9-_]*)\}(.*?)\{/\}%si',$template,$matches,PREG_SET_ORDER))
{
foreach( $matches as $match )
{
if(isset($data[$match[1]]) and is_array($data[$match[1]]) === true)
{
$replacements = array();
foreach( $data[$match[1]] as $iteration )
{
$replacements[] = parse_template($match[2],$iteration);
//$replacements[] = parse_template($match[2],array_merge($data,$iteration)); // You can choose this behavior
}
$template = str_replace($match[0],implode(PHP_EOL,$replacements),$template);
}
}
}
// Individual Tags
if(preg_match_all('/\{([a-z0-9-_]*)\}/i',$template,$matches,PREG_SET_ORDER))
{
foreach( $matches as $match )
{
if(isset($data[$match[1]]))
{
$template = str_replace($match[0],$data[$match[1]],$template);
}
}
}
return $template;
}