Convert all HTML entities in database to actual characters


/ Published in: PHP
Save to your folder(s)

Sometimes a legacy database will have HTML entities stored in it.

This function converts them. Note that this function assumes you have a database abstraction layer, and may need to be modified to connect to YOUR database.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. $connector = new DbConnector(); // Change to YOUR db connection class.
  4. $query = "SET NAMES 'utf8'"; // Put MySQL connection into UTF8 mode.
  5. $connector->query($query); // Change to YOUR db query execution method.
  6.  
  7. $tables = array('table1','table2'); // Change to YOUR table names.
  8. foreach($tables as $table)
  9. {
  10.  
  11. $sql = "SELECT * FROM {$table}";
  12. $rows = $connector->query($sql); // Change to YOUR db query execution method.
  13. while($row = mysql_fetch_assoc($rows))
  14. {
  15. $new = array();
  16. foreach($row as $key => $data)
  17. {
  18. $new[$key] = $connector->escapeString(html_entity_decode($data, ENT_QUOTES, 'UTF-8')); // Change to YOUR db escape execution method.
  19. }
  20. array_shift($new);
  21. $new_string = "";
  22. $i = 0;
  23. foreach($new as $new_key => $new_data)
  24. {
  25. if($i > 0) { $new_string.= ", "; }
  26. $new_string.= $new_key . "='" . $new_data . "'";
  27. $i++;
  28. }
  29. $sql = "UPDATE {$table} SET " . $new_string . " WHERE id='" . $row['id'] . "'";
  30. $connector->query($sql); // Change to YOUR db query execution method.
  31. }
  32. }
  33.  
  34. ?>

URL: http://stackoverflow.com/questions/2838245/how-to-remove-htmlentities-values-from-the-database

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.