Solved

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

Posted on 2016-10-13
4
33 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 51

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

706 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

20 Experts available now in Live!

Get 1:1 Help Now