Solved

Strip HTML Entities and convert <br> Entity to \n

Posted on 2016-10-13
4
72 Views
Last Modified: 2016-10-14
I am pulling info out of a database that has html entities. I need to convert <br> to \n and remove all other entities to be able to add content to an email body without the html.

12:00pm - Some &amp; Text goes here &lt;p&gt;&lt;strong&gt;TUESDAY - NOVEMBER 1&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;12:00PM - 5:00PM&lt;/strong&gt;&lt;br /&gt;

Open in new window

0
Comment
Question by:Jeff
  • 2
4 Comments
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 41842174
Take a look at strip_tags()

You would do the following
$html = strip_tags($html, '<br>');
$html = preg_replace('/\<br\s*\/?\>/',"\n", $html);

Open in new window

0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41842240
You probably want to use html_entity_decode() to turn the entities back into HTML tags.  Then the strip_tags() and other functions will probably do what you want.
https://iconoun.com/demo/temp_jeffey48.php
<?php // demo/temp_jeffey48.php
/**
 * https://www.experts-exchange.com/questions/28976194/Strip-HTML-Entities-and-convert-br-Entity-to-n.html
 *
 * http://php.net/manual/en/function.html-entity-decode.php
 * http://php.net/manual/en/function.nl2br.php#86678
 */
error_reporting(E_ALL);

// TEST DATA COPIED FROM THE POST AT E-E
$str = '12:00pm - Some &amp; Text goes here &lt;p&gt;&lt;strong&gt;TUESDAY - NOVEMBER 1&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;12:00PM - 5:00PM&lt;/strong&gt;&lt;br /&gt;';

// TRANSFORM THE DATA BACK INTO HTML
$new = html_entity_decode($str);

// REMOVE THE UNWANTED TAGS, PRESERVING THE BREAK TAGS
$new = strip_tags($new, '<br>');

// REPLACE THE HTML BREAK TAGS WITH NEWLINE CHARACTERS
$new = preg_replace('/\<br(\s*)?\/?\>/i', PHP_EOL, $new);

// SHOW THE WORK PRODUCT
echo '<pre>';
echo $new;

Open in new window

1
 

Author Comment

by:Jeff
ID: 41843994
Julian, Thanks for the quick response. I couldn't quite get the solution to work. The entities needed to be converted to HTML first, then strip and replace.

Ray, Your solution worked perfectly. Thanks you.
0
 

Author Closing Comment

by:Jeff
ID: 41843996
Thanks Ray.
1

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP Mail error 3 28
Html value of radio 14 31
Ajax and PHP 4 31
Pull Variable from URL  Use php template 1 13
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question