2

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.

2
  • 1
    you can use php strip_tags($text, '<br>') Jul 31, 2016 at 18:08
  • yeah but there are 15000 product description in table and i want to update also them then?
    – webbed
    Jul 31, 2016 at 18:10

3 Answers 3

8

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...

7
  • The OP stated: I can do this with strip_tag but I don't understand how ! I think he needs the full code !! Jul 31, 2016 at 18:09
  • @IsmailRBOUH sorry, I don't get it. I showed how to do it, a working example and quoted the docs. With the given info, that's what there is...
    – FirstOne
    Jul 31, 2016 at 18:11
  • yeah but there are 15000 product description in table and i want to update also them then?
    – webbed
    Jul 31, 2016 at 18:11
  • for now i used to create a function in mysql but its removing all html tags in table.
    – webbed
    Jul 31, 2016 at 18:12
  • I don't think the OP by I don't understand how meant the function parameters :D Jul 31, 2016 at 18:15
4

Create function using below code :

DROP FUNCTION IF EXISTS fnStripTags;
DELIMITER |
CREATE FUNCTION fnStripTags( Dirty varchar(4000) )
RETURNS varchar(4000)
DETERMINISTIC 
BEGIN
  DECLARE iStart, iEnd, iLength int;
    WHILE Locate( '<', Dirty ) > 0 And Locate( '>', Dirty, Locate( '<', Dirty )) > 0 DO
      BEGIN
        SET iStart = Locate( '<', Dirty ), iEnd = Locate( '>', Dirty, Locate('<', Dirty ));
        SET iLength = ( iEnd - iStart) + 1;
        IF iLength > 0 THEN
          BEGIN
            SET Dirty = Insert( Dirty, iStart, iLength, '');
          END;
        END IF;
      END;
    END WHILE;
    RETURN Dirty;
END;
|
DELIMITER ;

And execute below query :

SELECT fnStripTags('this <html>is <b>a test</b>, nothing more</html>');
1
  • This doesn't address the user's issue of br tags
    – Tah
    Jan 4, 2017 at 4:57
2

for just one tag you can do this query

UPDATE `TABLE` SET column1 = REPLACE(column1, '<br>', '');

or make function for strip all html tags like this example

CREATE FUNCTION `strip_tags`($str text) RETURNS text
BEGIN
    DECLARE $start, $end INT DEFAULT 1;
    LOOP
        SET $start = LOCATE("<", $str, $start);
        IF (!$start) THEN RETURN $str; END IF;
        SET $end = LOCATE(">", $str, $start);
        IF (!$end) THEN SET $end = $start; END IF;
        SET $str = INSERT($str, $start, $end - $start + 1, "");
    END LOOP;
END;

this function found from

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.