我搜索了谷歌,但没有找到适合我的问题,或者我用错误的词搜索。
在我阅读的许多主题中,聪明的模板是解决方案,但我不会使用smarty,因为它对于这个小项目来说很重要。
我的问题:
我有一个CSV文件,这个文件只包含HTML和PHP代码,它是一个简单的html模板文档,例如我用来生成动态图像链接的phpcode。
我想阅读这个文件(有效),但我如何处理这个文件中的phpcode,因为phpcode显示为原样。我在CSV文件中使用的所有变量仍然有效。
简短版
如何在CSV文件中处理,打印或回显phpcode。
非常感谢,
抱歉我的英语不好
答案 0 :(得分:0)
格式化您的评论,您有以下代码:
$userdatei = fopen("selltemplate/template.txt","r");
while(!feof($userdatei)) {
$zeile = fgets($userdatei);
echo $zeile;
}
fclose($userdatei);
// so i read in the csv file and the content of csv file one line:
// src="<?php echo $bild1; ?>" ></a>
这假设在其他地方定义了$bild1
,但尝试在while
循环中使用这些函数来解析并输出你的html / php:
$userdatei = fopen("selltemplate/template.txt","r");
while(!feof($userdatei)) {
$zeile = fgets($userdatei);
outputResults($zeile);
}
fclose($userdatei);
//-- $delims contains the delimiters for your $string. For example, you could use <?php and ?> instead of <?php and ?>
function parseString($string, $delims) {
$result = array();
//-- init delimiter vars
if (empty($delims)) {
$delims = array('<?php', '?>');
}
$start = $delims[0];
$end = $delims[1];
//-- where our delimiters start/end
$php_start = strpos($string, $start);
$php_end = strpos($string, $end) + strlen($end);
//-- where our php CODE starts/ends
$php_code_start = $php_start + strlen($start);
$php_code_end = strpos($string, $end);
//-- the non-php content before/after the php delimiters
$pre = substr($string, 0, $php_start);
$post = substr($string, $php_end);
$code_end = $php_code_end - $php_code_start;
$code = substr($string, $php_code_start, $code_end);
$result['pre'] = $pre;
$result['post'] = $post;
$result['code'] = $code;
return $result;
}
function outputResults($string) {
$result = parseString($string);
print $result['pre'];
eval($result['code']);
print $result['post'];
}
答案 1 :(得分:0)
在CSV
文件中包含应该被解析并且可能使用eval
执行的PHP代码听起来对我来说非常危险。
如果我说得对,你只想在CSV文件中使用动态参数吗?如果是这种情况,并且您不想在您的应用中实施完整的模板语言(如Mustache,Twig或Smarty),则可以进行简单的搜索并替换事物。
$string = "<img alt='{{myImageAlt}}' src='{{myImage}}' />";
$parameters = [
'myImageAlt' => 'company logo',
'myImage' => 'assets/images/logo.png'
];
foreach( $parameters as $key => $value )
{
$string = str_replace( '{{'.$key.'}}', $value, $string );
}