0

i am working on a function that will convert HTML to Plaintext version using php.I have tried with strip_tags() as follows,

  $html='<style type="text/css">
  @media only screen and (max-width: 480px) {
    .message_mobile {
        width: 100% !important;
    }
  }
 </style>
<p class="message_mobile"> sample Text</p>';
$plain_text       =strip_tags($html);
echo $plain_text;

But it will create output like,

 @media only screen and (max-width: 480px) {
    .message_mobile {
        width: 100% !important;
    }
  }

  sample Text

But i don't need the content inside <style> tag.How to do this? And i have another problem,When i try to strip tags with a table,It will create unwanted line brakes.How to resolve these problems? Is there any good methods for create plain text from HTML?

Shijin TR
  • 7,516
  • 10
  • 55
  • 122

3 Answers3

0

Use this function:

<?php

function strip_html_tags($str){
    $str = preg_replace('/(<|>)\1{2}/is', '', $str);
    $str = preg_replace(
        array(// Remove invisible content
            '@<head[^>]*?>.*?</head>@siu',
            '@<style[^>]*?>.*?</style>@siu',
            '@<script[^>]*?.*?</script>@siu',
            '@<noscript[^>]*?.*?</noscript>@siu',
            ),
        "", //replace above with nothing
        $str );
    $str = replaceWhitespace($str);
    $str = strip_tags($str);
    return $str;
} //function strip_html_tags ENDS

//To replace all types of whitespace with a single space
function replaceWhitespace($str) {
    $result = $str;
    foreach (array(
    "  ", " \t",  " \r",  " \n",
    "\t\t", "\t ", "\t\r", "\t\n",
    "\r\r", "\r ", "\r\t", "\r\n",
    "\n\n", "\n ", "\n\t", "\n\r",
    ) as $replacement) {
    $result = str_replace($replacement, $replacement[0], $result);
    }
    return $str !== $result ? replaceWhitespace($result) : $result;
}


$html='<style type="text/css">
  @media only screen and (max-width: 480px) {
    .message_mobile {
        width: 100% !important;
    }
  }
 </style>
<p class="message_mobile"> sample Text</p>';
$plain_text = strip_html_tags($html);
echo $plain_text;
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

The function you're looking for is htmlspecialchars.

This code:

<?php
    $htmltag  = '
    <style type="text/css">
        @media only screen and (max-width: 480px) {
            .message_mobile {
                width: 100% !important;
            }
        }
    </style>
    <p class="message_mobile"> sample Text</p>';
    echo "<pre>".nl2br(htmlspecialchars($htmltag))."</pre>";
?>

will create this output on your website:

<style type="text/css">

    @media only screen and (max-width: 480px) {

        .message_mobile {

            width: 100% !important;

        }

    }

</style>

<p class="message_mobile"> sample Text</p>
Paul L.
  • 50
  • 1
  • 9
0

You can use classes for creating Plain text from HTML.

Visit this link this might help you out. Converting HTML to plain text in PHP for e-mail

Classes: http://www.howtocreate.co.uk/php/html2texthowto.html

Try this, it helps me

http://code.google.com/p/iaml/source/browse/trunk/org.openiaml.model.runtime/src/include/html2text

Community
  • 1
  • 1
Rohit Nigam
  • 708
  • 8
  • 23