-1
我想获得一个变量值并将其添加到字符串中以输出html元素。这里是我的代码:变量失去其字符串属性
// String
$str = 'Some validations [IF TEST]<a href="text.html">firstValue</a>[ELSEIF TEST1]secondValue[/IF] in [IF OK]thirdValue[/IF] end of string.';
// Regex
$do = preg_match_all("/\[IF (.*?)\](.*?)(\[\/IF\]|\[ELSEIF(.*?)\](.*?)\[\/IF\])/i", $str, $matches, PREG_SET_ORDER);
$str = str_replace("[IF ". $matches[0][1] ."]", "<?php if($". $matches[0][1] ."){ echo('".$matches[0][2]."');} ?>", $str);
echo $str;
但是,出于某些原因,当我使用$比赛[0] [2],它打破了我的字符串,给出了这样的结果在浏览器中:
Some validations firstValue');} ?>firstValue[ELSEIF TEST1]secondValue[/IF] in [IF OK]thirdValue[/IF] end of string.
和这在源:
Some validations <?php if($TEST){ echo('<a href="text.html">firstValue</a>');} ?><a href="text.html">firstValue</a>[ELSEIF TEST1]secondValue[/IF] in [IF OK]thirdValue[/IF] end of string.
谢谢大家!
开始接受答案:http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work –