Solved

convert html into regular text

Posted on 2011-03-03
11
389 Views
Last Modified: 2012-05-11
nl2br(strip_tags($str, '<br>'));



can convert

<br>
<p>hi how are you<p>
<b>bold</b>


into regular text

using a php function



but I want to convert a more complex input


<br>
<p>hi how are you
<h1>h1 tag</h1>
<h2>h2 tag</h2>
<h3>h3 tag</h3>

<p>
<b>bold</b>
0
Comment
Question by:rgb192
[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
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35032318
I'm not a PHP programmer, but I've done that in C# with the following regular expression:
<(.|\n)*?>

I think in PHP would be something like this:
preg_replace("/<(.|\n)*?>/", "", $str);

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 35032319
Please show us exactly what you want the output to be from converting this code snippet.
<br>
<p>hi how are you
<h1>h1 tag</h1>
<h2>h2 tag</h2>
<h3>h3 tag</h3>

<p>
<b>bold</b>

Open in new window

0
 
LVL 4

Expert Comment

by:ute_arbeit
ID: 35032364
You can find many free html2text converters if you are googling around.
For example here is one script (under Eclipse Public License) which looks quite simple and reasonable.

Just use it like this:
require("html2text.php");
...
$text = convert_html_to_text ($html);

Open in new window

0
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 

Author Comment

by:rgb192
ID: 35032368
$before='
<br>
<p>hi how are you
<h1>h1 tag</h1>
<h2>h2 tag</h2>
<h3>h3 tag</h3>

<p>
<b>bold</b>
';




output:


hi how are you
h1 tag
h2 tag
h3 tag

bold




and I dont know how to but $before in

$str = <<<ENDSTRING
ENDSTRING


(that is from your previous answer)
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 300 total points
ID: 35032397
See http://www.laprbass.com/RAY_temp_rgb192.php
<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);


// TEST DATA
$before='
<br>
<p>hi how are you
<h1>h1 tag</h1>
<h2>h2 tag</h2>
<h3>h3 tag</h3>

<p>
<b>bold</b>
';

/* DESIRED OUTPUT

hi how are you
h1 tag
h2 tag
h3 tag

bold
*/

// PROCESS THE TEST DATA
$after = nl2br(strip_tags($before));
echo $after;

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 35032413
This is called heredoc syntax.

$str = <<<ENDSTRING
 -- heredoc stuff is contained in
 -- the lines before the ENDSTRING
ENDSTRING;

The rules for heredoc are shown here:
http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

It is VERY useful for things like forms, etc.  Variable substitution occurs inside heredoc strings and quotes do not need to be escaped.
0
 
LVL 4

Assisted Solution

by:ute_arbeit
ute_arbeit earned 200 total points
ID: 35032489
Sorry, I forgot the url in my previous post. Here is the same post again:

You can find many free html2text converters if you are googling around.
For example here http://journals.jevon.org/users/jevon-phd/entry/19818 is one script (under Eclipse Public License) which looks quite simple and reasonable.

Just use it like this:
require("html2text.php");
...
$text = convert_html_to_text ($html);

Open in new window

0
 
LVL 7

Expert Comment

by:Vimal DM
ID: 35034074
Hai,

Just use the function " strip_tags($str) " need not to pass  another arguments
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 35036946
@vimalmaria: Without nl2br() the output is: hi how are you h1 tag h2 tag h3 tag bold because HTML treats new line characters (indeed all white space) like blanks.

That is somewhat different from:
hi how are you
h1 tag
h2 tag
h3 tag


bold
0
 

Author Comment

by:rgb192
ID: 35037090


Thanks, I have a similar question

I want to send the £  symbol without utf-8 encoding


http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_26864165.html
0
 

Author Closing Comment

by:rgb192
ID: 35037099
Thanks, I have a similar question

I want to send the £  symbol without utf-8 encoding


http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_26864165.html
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

688 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