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.
如何从mysql数据中删除除了<br>的html标签?
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
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...
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报