Solved

convert html into regular text

Posted on 2011-03-03
11
377 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 108

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
 

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 108

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 108

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 108

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now