我是PHP的新手,并且很难尝试清理此函数创建的输出。任何反馈都将不胜感激。
功能如下:
function getTemplateBreadcrumbs($path, $title){
$path = str_replace("/", "/", $path);
$breadcrumbs = "<ul>\n";
$breadcrumbs .= " <li><a href=\"/\">Home</a></li>\n";
$directories = explode("/", $path);
$tempPath = "/";
for($x=1; $x < count($directories)-2; $x++){
if($directories[$x] != null){
$tempPath .= $directories[$x] . "/";
$linkText = getPageVariable("pageTitle", $_SERVER["DOCUMENT_ROOT"] . $tempPath . "index.php");
if($linkText == null){
$linkText = ucwords(str_replace("-", " ", $directories[$x]));
}
$breadcrumbs .= " <li><a href=\"" . $tempPath . "\">$linkText</a></li>\n";
}
}
$breadcrumbs .= " <li>" . $title . "</li>\n";
$breadcrumbs .= "</ul>\n";
return $breadcrumbs;
}
答案 0 :(得分:1)
将filter_var()
与FILTER_SANITIZE_URL
flag一起使用。
$url = filter_var($tempPath , FILTER_SANITIZE_URL);
答案 1 :(得分:1)
您可以使用echo
,print_r()
,var_dump()
进行调试
我用:
echo htmlspecialchars(print_r($var));
如何使用的示例:
function getTemplateBreadcrumbs($path, $title){
//rest of the code
echo '<pre>';
echo htmlspecialchars(print_r($breadcrumbs));
echo '</pre>';
return $breadcrumbs;
}
如果你想从代码的另一部分进行测试:
$teste = getTemplateBreadcrumbs('/home/index', 'Home Page');
echo '<pre>';
echo htmlspecialchars(print_r($teste));
echo '</pre>';