Code Snippets

Strip tags and HTML from a string with PHP | php

I wrote this while having to do a quick hack on a car site. The DB contained some really ugly HTML that I needed to strip in a hurry. (Of course, later it should be edited with Mysql REPLACE). But here it is:


// // kit - ugly hack to strip <b> and <strong> from description until we can update DB

// // 1) replace all <b> and <strong> with <h3>, and also replace nbsp's with a line break

$cars['car_description'] = str_ireplace(array('<b>', '</b>', '<strong>', '</strong>', '&nbsp;'), array('<h3>', '</h3>', '<h3>', '</h3>', '<br/>'), $cars['car_description']);

// // 2) Strip all HTML tags, EXCEPT the below ones which remain acceptable
$cars['car_description'] = strip_tags($cars['car_description'], '<h3><ul><li><p><span><br>');

Published: Wed 10th January 2018
||COMMENTS|| This site proudly uses PrismJS to display code snippets

If you like this page, please share it.

Code Links - PHP, JS, CSS, Bootstrap, Bash, Emmet, IntelliJ, Sublime etc.

My personal list of links which I keep here publicly incase they help someone else one day.

Code Snippets

Snippets of PHP code I have gathered over the years. I keep them here for my own repository but also to share with others. I always link back when I remember the source.

My Github Repos

A work in progress. I am going to make as many modules that I write in my devbox public as possible.

Kit's Homepage

This site was originally intended as a test bed for code. It now includes my blog about business and powerlifting, as well as games for Spanish language learning

© 2018