0

任何建议都会有所帮助,谢谢

我正在尝试用内容清理一段 html 标签,但由于某种原因它无法正常工作

<?php

function cleaning($vclean)
{
$vclean = strip_tags($vclean);
$vclean = rtrim($vclean);
$vclean = trim($vclean);

$vclean = str_replace("  ", "", $vclean);
$vclean = preg_replace('/\s\s+/', ' ', $vclean);
$vclean = str_replace("'", "", $vclean);

echo "function is cleaning<br>";

}


cleaning($any_variable);


?>
4

2 回答 2

1

这是你想要的:

<?php

function cleaning($vclean)
{
$vclean = strip_tags($vclean);
$vclean = rtrim($vclean);
$vclean = trim($vclean);

$vclean = str_replace("  ", "", $vclean);
$vclean = preg_replace('/\s\s+/', ' ', $vclean);
$vclean = str_replace("'", "", $vclean);

return $vclean;

}


echo cleaning("function is cleaning<br>");


?>
于 2012-11-08T00:09:25.827 回答
0

也许您期望该函数清除原始变量?

像:

$variable = '<p>foobar</p>';
cleaning($variable);
echo $variable;

这不起作用,因为函数无法“看到”或更改在函数外部声明的变量。因此,您将看到 $variable 的旧值,而不是清理后的值。

如果是这种情况,您应该阅读有关变量范围的信息

至于实际解决方案,您应该return在函数末尾清除变量,并执行以下操作:

$variable = '<p>foobar</p>';
$variable = cleaning($variable);
echo $variable;
于 2012-11-08T00:18:25.430 回答