dongsisui7562 2016-07-31 18:02
浏览 380
已采纳

如何从mysql数据中删除除了<br>的html标签?

I have a field Product_description in mysql table, which has html tags. I want to update this field with removing html tags except br. I know I can do this with strip_tag but I don't understand how to accomplish this for the table data.

  • 写回答

3条回答 默认 最新

  • duancaozen6066 2016-07-31 18:08
    关注

    To remove all tags but <br> from a text, you can indeed use strip_tags:

    $var = strip_tags($html, '<br>');
    

    See an example here.

    From the docs:

    You can use the optional second parameter to specify tags which should not be stripped.



    To remove them from the persisted data (not meant to run multiple times), you can create a heavy running script to be used once and update the values. Like this: (You might want to use trim too)

    $con = new mysqli('localhost', 'username', 'password', 'database_name');
    $stmt = $con->query('SELECT * FROM table_name');
    while($row = $stmt->fetch_assoc()){
        $stmt2 = $con->prepare('UPDATE table_name set Product_description = ? WHERE Product_id = ?');
        $tmp = strip_tags($row['Product_description'], '<br>');
        $stmt2->bind_param("si", $tmp, $row['Product_id']);
        $stmt2->execute();
    }
    

    That script would be for you to run it just once, so it's (arguably) ok if it's slow...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?