str_replace() 无法使用带有搜索参数变量的 while 循环

str_replace() not working with while loop with variable for search argument

$query1 = mysqli_query($cone,"SELECT words FROM banlist ORDER BY words") or die(mysql_error());

while($row = mysqli_fetch_array($query1, MYSQLI_ASSOC)) {
    $fix = (string)$row['words'];                   
    $title = str_replace($fix, "-", $title);
}

echo $title;

var_dump($row) 的输出:

array(1) {
    ["words"]=> string(5) "hello "
}
array(1) {
    ["words"]=> string(5) "you "
}

它不会替换标题中的任何字符串但是如果我像 "hello" 一样直接将值放在 $fix 的位置那么它就可以正常工作。

通过与您进行一些故障排除,我们确定这是来自数据库字段的 space。要更正问题代码应更改为

$query1 = mysqli_query($cone,"SELECT words FROM banlist ORDER BY words") or die(mysql_error());

while($row = mysqli_fetch_array($query1, MYSQLI_ASSOC)) {
    $fix = trim($row['words']);                   
    $title = str_replace($fix, "-", $title);
}

echo $title;

理想情况下,字符串在插入数据库时​​应进行修剪,以防止出现此问题。