흠이 하나 너무 나를 위해 '바퀴를 개혁'문제와 같은 약간 것 같다,하지만 난 좀 그것을 좋아, 그래서 나는 주위를 연주 조금 있지만 정규식이 없으면 좀 더 일반적인 솔루션이 되길 원했기 때문에 사용자 정의 속성을 사용할 수 있습니다 (하지만 공백 값은 속성 값으로 사용하지 않음). 그래서 결국 다음과 같이 끝났습니다.
<?php
function use_template($search,$replace,$string, $options=array()) {
$searchdata = explode(" ", $search); //{download, text='Fancy'}
$template = substr($searchdata[0],1); // download
for ($i = 1; $i < sizeof($searchdata);$i++) {
$attribute = explode("=", $searchdata[$i]); //$attribute[0] = "text"; $attribute[1] = "'Fancy'}"
if (endsWith($attribute[1],'}')) {
$options[$attribute[0]] = substr($attribute[1], 0, -1);
} else {
$options[$attribute[0]] = $attribute[1];
}
}
$a = str_ireplace("{".$template."}",$replace,$string); // Hello, this is my {<a class="button">[text]</a>} button
foreach($options as $to_replace => $newval) {
$a = str_ireplace("[".$to_replace."]", $newval, $a); // Hello, this is my Fancy button
}
return $a;
}
function endsWith($haystack, $needle)
{
return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
}
$download = '<a class="button" style="background-color: [color];">[text]</a>';
$search = "{download text='Fancy' color=red}";
$string = "Hello, this is my {download} button!";
$options = array("text" => "Download", "color" => "#000000");
$string= use_template($search,$download,$string,$options);
echo $string;
?>
아이디어는 훌륭하지만 왜 바퀴를 재발 명합니까? – ex3v
설치가 쉽기 때문에 사용자는 기본적으로 html/css를 사용하여 자신의 템플릿을 만들 수 있지만 특정 것들은이 태그를 사용하여 동일하게 만들어집니다. –