Solved

convert html into regular text

Posted on 2011-03-03
11
388 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
$_SERVER Variable question 31 56
while read ID; do 4 58
How to remove  "0":  in a json string 4 17
P3P validator , ASP, PHP ,Https and Http links 3 12
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

726 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