[英]How to check if string contains any specified words then pass an error out of the function
我一直在使用以下代码,以便在我的网站中快速有效地添加BB代码。
function replace($text) {
//User Emotions
$text = str_replace(":)", "<img src=\"smiles/cool.gif\">", $text);
//User formatting
$text = str_replace("[center]", "<center>", $text);
$text = str_replace("[/center]", "</center>", $text);
$text = str_replace("[colour=red]", "<font color = red>", $text);
$text = str_replace("[/colour]", "</font>", $text);
$text = str_replace("[colour=blue]", "<font color = blue>", $text);
$text = str_replace("[/colour]", "</font>", $text);
$text = str_replace("[colour=green]", "<font color = green>", $text);
$text = str_replace("[/colour]", "</font>", $text);
$text = str_replace("[colour=orange]", "<font color = orange>", $text);
$text = str_replace("[/colour]", "</font>", $text);
$text = str_replace("[colour=white]", "<font color = white>", $text);
$text = str_replace("[/colour]", "</font>", $text);
$text = str_replace("[colour=black]", "<font color = black>", $text);
$text = str_replace("[/colour]", "</font>", $text);
$text = str_replace("[colour=code]", "<font color = code>", $text);
$text = str_replace("[/colour]", "</font>", $text);
$text = str_replace("[b]", "<strong>", $text);
$text = str_replace("[/b]", "</strong>", $text);
$text = str_replace("[i]", "<i>", $text);
$text = str_replace("[/i]", "</i>", $text);
$text = str_replace("[u]", "<u>", $text);
$text = str_replace("[/u]", "</u>", $text);
$text = str_replace("[move]", "<marquee>", $text);
$text = str_replace("[/move]", "</marquee>", $text);
$text = str_replace("[img]", "<img border = \"0\" src = ", $text);
$text = str_replace("[/img]", ">", $text);
$text = str_replace("[code]", "<div id=code>", $text);
$text = str_replace("[/code]", "</div>", $text);
$text = str_replace(array("\r\n", "\n", "\r"), '<br />', $text);
//Racial Hatred Blocking
include("snippets/racial_violations.php");
return $text;
}
我想问的问题是我如何检查包含的$ text是否说:
通过将我的文本var传递给函数(类似于我上面的方式),但不替换像str_replace那样的东西。 我想从函数传递一个错误,所以我可以使用:
if($text_error == 1){ echo "text does not contain foo, yar or bar";}
else ( echo "text CONTAINS foo, yar or bar";}
$ text_error可以是0或1,如果文本包含三个指定单词之一,则会分配此值。
希望我已经充分解释了这一点!
您也许可以编辑replace()
函数,在开头添加对任何此类单词的检查。
function replace($text) {
$bannedWords = array('foo', 'yar', 'bar') ;
foreach ($bannedWords as $bannedWord) {
if (strpos($text, $bannedWord) !== false) {
return false ;
}
}
//..rest of str_replaces below here
return $text
}
在这种情况下,如果找到其中一个单词,该函数将返回布尔值false
。 无论您在哪里调用replace()
都可以检查它。 如果没有找到,那么在替换任何BBcode之后将返回文本,如前所述。
所以,
$replacedText = replace($text) ;
if ($replacedText === false) {
//$text includes one of the bad words, act accordingly to inform the user
}
else {
//$text was ok, and has its bbcode replaced, use it accordingly
}
至于如何更有效地实现上面的代码: str_replace
也接受数组作为它的前两个参数:
$replace = array('[b]' => '<strong>', '[i]' => '<i>', ...);
$text = str_replace(array_keys($replace), array_values($replace), $text);
您可以将额外的变量传递给函数:
function replace($text, &$error) {
并在以后设置它
$error = "text does not contain foo, yar or bar";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.