Tips for deleting html tags to obtain tags that can be embedded in plain text

Source: Internet
Author: User
This article mainly introduces how to delete html tags to process nested tags in plain text. For more information, see the following method: the source code in THinkphp, but I modified it.

The code is as follows:


/*
* @ Description: delete HTML tags to obtain plain text. Can process nested labels
*
*/
Class deleteHtmlTags {

Private $ filename;

Function _ construct ($ filename = 'C:/AppServ/www/text.txt '){
$ This-> filename = $ filename;
}

/**
* Delete html tags to obtain plain text. Nested tags can be processed. the restriction is that the attribute values in tags 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 modified part is also above, but commented out. I personally think this method is better than regular expressions.

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.