Remove HTML tags get plain text to handle nested tags _php tips

Source: Internet
Author: User
The method basically comes from the source code in the thinkphp, but has been modified by me
Copy Code code as follows:

<?php
/*
* @Description: Delete HTML tags and get plain text. can handle nested labels
*
*/
Class deletehtmltags{

Private $filename;

function __construct ($filename = ' c:/appserv/www/text.txt ') {
$this->filename = $filename;
}

/**
* Delete HTML tags to get plain text. You can handle nested labels, but the limitation is that the property values in the tag are deleted
* @access Public
* @param string $string The HTML to be processed
* @return String
*/
Public Function Deletehtmltags () {
$content = $this->contentget ();
while (Strstr ($content, ' > ')) {
$currentBegin = Strpos ($content, ' < ');
$currentEnd = Strpos ($content, ' > ');
$cha = $currentEnd-$currentBegin-1;
$tmpStringBegin = @substr ($content, 0, $currentBegin);
$tmpStringMiddle = @ substr ($content, $currentBegin + 1, $cha);
$tmpStringEnd = @substr ($content, $currentEnd + 1, strlen ($content));
$content = $tmpStringBegin. $tmpStringMiddle. $tmpStringEnd;
$content = $tmpStringBegin. $tmpStringEnd;
}
return $content;
}

Private Function Contentget () {
$FD = fopen ($this->filename, ' R ');
$content = Fread ($FD, FileSize ($this->filename));
Fclose ($FD);
return $content;
}
}

$deleteHtml = new Deletehtmltags ();
$content = $deleteHtml->deletehtmltags ();
Echo $content;
?>

The revision is also on the top, but the comment is off. Personally, I think this method is better than using regular-type methods.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.