친구 (지금 휴가 중이신 분)에게 도움을 받았지만 preg_replace
검색에 문제가 있습니다. 나는 이유를 모르지만, 문자열을 잘못 대체 한 것이므로 다음에 대체해야 할 영향에 대한 노크가 있습니다.preg_replace가 잘못 대체되었습니다.
기본적으로 이것은 템플릿 내에서 'if'및 'else'쿼리를 처리하는 템플릿 클래스 내에 있습니다.
function if_statement($a, $b, $if, $type, $else = NULL){
if($type == "1" && is_numeric($a) && is_numeric($b)){
$statement = ($a === $b) ? $if : $else;
} else if($type == "1"){
$statement = ($a == $b) ? $if : $else;
} else if($type == "2"){
$statement = ($a != $b) ? $if : $else;
}
return stripslashes($statement);
}
$output = file_get_contents("template.tpl");
$replace = array(
'#\<if:"\'(.*?)\' == \'(.*?)\'"\>(.*?)\<else\>(.*?)\<\/endif\>#sei',
'#\<if:"\'(.*?)\' == \'(.*?)\'"\>(.*?)\<\/endif\>#sei'
);
$functions = array(
"if_statement('\\1', '\\2', '\\3', '1', '\\4')",
"if_statement('\\1', '\\2', '\\3', '1')"
);
$output = preg_replace($replace, $functions, $output);
echo $output;
템플릿 :
<HTML>
<head>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" />
<title>Site Title</title>
<link rel="stylesheet" type="text/css" media="screen" href="common.css" />
<if:"'{ISADMIN}' == '1'">
<link rel="stylesheet" href="admin-bar.css" type="text/css" media="all" />
</endif>
</head>
<body>
<if:"'{TODAY}' == 'Monday'">Today is Monday<else>Today is not Monday</endif>
<if:"'1' == '2'">1 equals 2!<else>1 doesn't equal 2</endif>
</body>
</html>
전류 출력 아래 것이다 : 상기에서
<HTML>
<head>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" />
<title>Site Title</title>
<link rel="stylesheet" type="text/css" media="screen" href="common.css" />
<link rel="stylesheet" href="admin-bar.css" type="text/css" media="all" />
**</endif>**
</head>
<body>
**<if:"'{TODAY}' == 'Monday'">**Today is Monday
1 doesn't equal 2
</body>
</html>
, 굵은/애스 트릭스 makred 부분의 출력에 존재하지 않아야 , 그리고 오늘은 월요일이 아닙니다. 관리자가 로그인하는 동안 admin-bar.css
파일이 올바르게 포함되었지만 어떤 이유에서 </endif>
태그를 가져 오지 않았습니다. 다음 문장 대신 <else>
태그를 찾은 것 같습니다. words, preg_replace
가 잘못된 물건과 일치했습니다! 그리고 따라서 2 번째 <if>
성명서를 접하지 못했습니다. 나는 심지어 수동으로 문 (단지 확인)에 데이터를 넣어, 그래서 그들은 문제가되지 않습니다 ...
나는 이유는 모르겠지만, -
{BRACKET}
태그
preg_replace
은 올바른 순서를 찾아서 작동하지 않습니다. 누구든지 신선한 눈을 만들거나 손을 빌려줄 수 있다면 감사 할 것입니다.
감사합니다.
도움과 설명을 보내 주셔서 감사합니다. Tim! 초기 '
MrJ
물론, 나는 나의 대답을 편집했다. –