当我这样做时,我的SQL代码可以工作 phpmyadmin,但是当我在我的php脚本中运行它时,它没有返回任何我认为可能是编码的内容(是utf8(,所以我将其更改为ansi但没有区别。你们中的一个人能不能看看它有什么问题吗?(这是一个从Wordpress帖子中删除图像的脚本(
代码:
$query = "SELECT * FROM `wp_posts` where post_content like'%<img%'>'" or die("Error in the consult.." . mysqli_error($link));//THIS IS THE QUERY THAT DOES NOT WORK
$result = $link->query($query);
/*function filter($toFilter)
{
$pattern = "/<img.*?>/";
$filtered = "";
if(preg_match($pattern,$toFilter))
{
$filtered = preg_replace($pattern,'',$toFilter);
}
return (string)$filtered;
}
*/
while($row = mysqli_fetch_array($result)) {
$id = $row['ID'];
$toFilter = $row['post_content'];
$toFilter = (string)$toFilter;
$pattern = "/<img.*?>/";
$filtered = "";
if(preg_match($pattern,$toFilter))
{
$filtered = preg_replace($pattern,'',$toFilter);
}
$filtered = (string)$filtered;
$link->query("update wp_posts set post_content = $filtered where ID = $id;");
echo 'this works';
}
- 你必须在
'%<img%'>'
中逃脱这个斜杠(除非你的目的是逃避>
? - 图像没有结束标记(http://www.w3.org/TR/html401/struct/objects.html#edef-IMG(,因此您不妨查找起始标记
- 继续使用 utf8
-
$pattern = "/<img.*?>/";
可以简单地$pattern = "/<img.*>/";
.*
表示 0 或更多,而 ? 表示可选,因此几乎是多余的