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

问题描述 投票:0回答:1
$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位置那么它可以正常工作。

php str-replace
1个回答
0
投票

通过一些故障排除,我们确定它是来自数据库字段的空间。要更正问题,应将代码更改为

$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;

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

© www.soinside.com 2019 - 2024. All rights reserved.