简体   繁体   English

PHP功能建议

[英]php function advice needed

any advice would be helpful, thank you 任何建议都会有所帮助,谢谢

i'm trying to clean a piece of html tags with contents but for some reason it's not working properly 我正在尝试清理包含内容的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);


?>

Is this what you were intending: 这是您想要的:

<?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>");


?>

Perhaps you are expecting the function to clean the original variable? 也许您期望该函数清除原始变量?

Like: 喜欢:

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

This does not work, because the function cannot 'see' or change a variable that is declared outside the function. 这是行不通的,因为函数无法“查看”或更改在函数外部声明的变量。 Because of that, you will see the old value of $variable, not the cleaned one. 因此,您将看到$ variable的旧值,而不是清除后的值。

If this is the case, you should read about Variable scopes 在这种情况下,您应该阅读有关变量作用域的信息。

As for the practical solution, you should return the cleaned variable in the end of the function, and do the following: 至于实际的解决方案,您应该在函数的末尾return cleaned变量,然后执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM