Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2016-10-13
4
Medium Priority
?
133 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 59

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
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)

721 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