Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在数据库中查找并替换重复字符串_Php_Mysql_String - Fatal编程技术网

Php 在数据库中查找并替换重复字符串

Php 在数据库中查找并替换重复字符串,php,mysql,string,Php,Mysql,String,我的表格中的某一行有问题的数据,如下面的字符串 [data attr1='2300.48' attr1='1' attr1='1116.95' attr1='60.75' attr1='9.22' attr1='' attr1='0' attr1='3593.39' attr1='119.05' attr1='25.96' attr1='27.98' attr1='36.32'] [data attr1='2300.48' attr1='1' attr1='1116.95' attr1='60

我的表格中的某一行有问题的数据,如下面的字符串

[data attr1='2300.48' attr1='1' attr1='1116.95' attr1='60.75' attr1='9.22' attr1='' attr1='0' attr1='3593.39' attr1='119.05' attr1='25.96' attr1='27.98' attr1='36.32']

[data attr1='2300.48' attr1='1' attr1='1116.95' attr1='60.75' attr1='9.22' attr1='' attr1='0' attr1='3593.39' attr1='119.05' attr1='25.96' attr1='27.98' attr1='36.32']

[data attr1='2300.48' attr1='1' attr1='1116.95' attr1='60.75' attr1='9.22' attr1='' attr1='0' attr1='3593.39' attr1='119.05' attr1='25.96' attr1='27.98' attr1='36.32']

[data attr1='2300.48' attr1='1' attr1='1116.95' attr1='60.75' attr1='9.22' attr1='' attr1='0' attr1='3593.39' attr1='119.05' attr1='25.96' attr1='27.98' attr1='36.32'] 
现在我只想用一个相同的字符串替换它们,如:

[data attr1='2300.48' attr1='1' attr1='1116.95' attr1='60.75' attr1='9.22' attr1='' attr1='0' attr1='3593.39' attr1='119.05' attr1='25.96' attr1='27.98' attr1='36.32']
请记住,
attr
的值是可变的

--更新--

它是如何存储在数据库中的 列名称为
content
,此列中的值为

the text of the article
 [data.... ]
 [data.... ]
 [data.... ]
内容的数据类型为
longtext

很抱歉,我无法生成正确的sql表结构


提前感谢您的帮助。

根据其他需要,您可以使用
DISTINCT
GROUP BY
删除重复值。例如:

select distinct mycolumn from mytable;
或:


如果要删除/删除重复行,可以使用

DELETE t 
FROM t 
LEFT JOIN t AS t2 ON (t.content = t2.content AND t.id > t2.id) 
WHERE t2.id;
之前:

SELECT * FROM t;
+----+---------+
| id | content |
+----+---------+
|  1 | abc     |
|  5 | abc123  |
|  9 | abc123  |
| 13 | abc     |
| 17 | abc     |
+----+---------+
之后:

SELECT * FROM t;
+----+---------+
| id | content |
+----+---------+
|  1 | abc     |
|  5 | abc123  |
+----+---------+

这是制作我作品的脚本

$link = mysqli_connect("SERVER", "root", "root");
    $db = mysqli_select_db($link, "test");
    $res = mysqli_query($link, "SELECT * FROM wp_posts WHERE post_content LIKE '%nutr-label servingsize%nutr-label servingsize%'");

    if($res){
        $i=1;
        while($row = mysqli_fetch_array($res)){
            $str1 = strrpos($row['post_content'],"[nutr-label servingsize='");
            $sub1 = substr($row['post_content'],$str1);
            $str2 = strrpos($row['post_content'],"']");

            $finalStr = substr($row['post_content'],$str1,$str2);
            $newPostContent = substr($row['post_content'], 0, strpos($row['post_content'],"[nutr-label servingsize='")).$finalStr;
            $up = mysqli_query($link,"UPDATE wp_posts SET post_content ='".addslashes($newPostContent)."' WHERE ID='".$row['ID']."'");
            $i++;
        }
    }else{
        echo mysqli_error();
    }

它们是如何存储在数据库中的?只有一个文本字段?请检查:请提供您的表结构是否所有这些重复字符串都由“\n”分隔?@user868766否没有“\n”
$link = mysqli_connect("SERVER", "root", "root");
    $db = mysqli_select_db($link, "test");
    $res = mysqli_query($link, "SELECT * FROM wp_posts WHERE post_content LIKE '%nutr-label servingsize%nutr-label servingsize%'");

    if($res){
        $i=1;
        while($row = mysqli_fetch_array($res)){
            $str1 = strrpos($row['post_content'],"[nutr-label servingsize='");
            $sub1 = substr($row['post_content'],$str1);
            $str2 = strrpos($row['post_content'],"']");

            $finalStr = substr($row['post_content'],$str1,$str2);
            $newPostContent = substr($row['post_content'], 0, strpos($row['post_content'],"[nutr-label servingsize='")).$finalStr;
            $up = mysqli_query($link,"UPDATE wp_posts SET post_content ='".addslashes($newPostContent)."' WHERE ID='".$row['ID']."'");
            $i++;
        }
    }else{
        echo mysqli_error();
    }