Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

convert html into regular text

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>
Avatar of wdosanjos
wdosanjos
Flag of United States of America image

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

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

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

Avatar of rgb192

ASKER

$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)
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Vimal DM
Hai,

Just use the function " strip_tags($str) " need not to pass  another arguments
@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
Avatar of rgb192

ASKER



Thanks, I have a similar question

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


https://www.experts-exchange.com/questions/26864165/I-want-to-send-the-symbol-without-utf-8-encoding.html
Avatar of rgb192

ASKER

Thanks, I have a similar question

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


https://www.experts-exchange.com/questions/26864165/I-want-to-send-the-symbol-without-utf-8-encoding.html