Solved

convert html into regular text

Posted on 2011-03-03
11
386 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
  • 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 109

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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 

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 109

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 109

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 109

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

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

856 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