?
Solved

convert html into regular text

Posted on 2011-03-03
11
Medium Priority
?
392 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 111

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
Technology Partners: 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!

 

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 111

Accepted Solution

by:
Ray Paseur earned 1200 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 111

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 800 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 111

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

762 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