PHP坏词替换评论系统

时间:2016-03-16 15:15:39

标签: php mysql sql database

我正在尝试使用php创建一个评论系统,用****替换评论者的坏词。我正在使用sql,并且有一个包含两列的数据库,一个包含坏词,另一个包含替换词(这是****)。到目前为止,我能够确定用户何时键入确定的坏词并为其检索替换词,但是我无法用替换词替换该坏词。我试图在$ goodWord = $ row ['replaceWord']之后做$ element = $ goodWord,说新的已识别的坏词应该被替换,但它什么都不做。我不确定如何在php中将数组中的元素设置为新值。有什么建议吗?

代码:

if(!empty($_GET["field1_name"])) {
    $field1_name = mysqli_real_escape_string($link, $_GET["field1_name"]);
    $field1_name_array = explode(" ",$field1_name);

    foreach($field1_name_array as $element){
        $query = "SELECT replaceWord FROM changeWord WHERE badWord = '$element' ";
        $query_link = mysqli_query($link,$query);
        if(mysqli_num_rows($query_link)>0){
            $row = mysqli_fetch_assoc($query_link);
            $goodWord = $row['replaceWord'];
            $element = $goodWord
        }
    }
}

1 个答案:

答案 0 :(得分:2)

if(!empty($_GET["field1_name"])) {
$field1_name = mysqli_real_escape_string($link, $_GET["field1_name"]);
$field1_name_array = explode(" ",$field1_name);
$newComment = '';
foreach($field1_name_array as $element){
    $query = "SELECT replaceWord FROM changeWord WHERE badWord = '$element' ";
    $query_link = mysqli_query($link,$query);
    if(mysqli_num_rows($query_link)>0){
        $row = mysqli_fetch_assoc($query_link);
        $goodWord = $row['replaceWord'];
        $element = $goodWord
    }
    $newComment = $newComment." ".$element; // append $element back to back to create modified comment
    }
   // now update the $newComment back to your comment Table
}