Solved

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

Posted on 2016-10-13
4
93 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 57

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 110

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to implement server side field validation and display customized error messages to the client.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn how to count occurrences of each item in an array.

738 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