Solved

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

Posted on 2016-10-13
4
46 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:jeffey48
  • 2
4 Comments
 
LVL 52

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 108

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:jeffey48
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:jeffey48
ID: 41843996
Thanks Ray.
1

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

914 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now