Parsing php uses regular expressions to solve the problem of collecting content Layout

Source: Internet
Author: User

The problem that is often encountered during collection is the problem of content formatting. After some time, I wrote a function that replaces html tags and styles with regular expressions, sharing them. Copy codeThe Code is as follows :/**
* Format the content
* @ Param string $ the content should be encoded in UTF-8 format.
* @ Return string
*! This function needs to enable the tidy extension.
*/
Function removeFormat ($ content ){
$ Replaces = array (
"/<Font. *?> /I "=> '',
"/<\/Font>/I" => '',
"/<Strong>/I" => '',
"/<\/Strong>/I" => '',
"/<Span. *?> /I "=> '',
"/<\/Span>/I" => '',
"/<Div. *?> /I "=>" <p> ",
"/<\/Div>/I" => "</p> ",
"/<! -- <. *?> * -->/I "=> '',
/* "/<Table. *?> /I "=>'', // do not enable table content
"/<\/Table>/I" => '',
"/<Tbody. *?> /I "=> '',
"/<\/Tbody>/I" => '',
"/<Tr. *?> /I "=> '<p> ',
"/<\/Tr>/I" => '</p> ',
"/<Td. *?> /I "=> '',*/
"/Style =. +? ['| \ "]/I" => '',
"/Class =. +? ['| \ "]/I" => '',
"/Id =. +? ['| \ "]/I" => '',
"/Lang =. +? ['| \ "]/I" => '',
// "/Width =. +? ['| \ "]/I" => '', // do not properly control the comment
// "/Height =. +? ['| \ "]/I" => '',
"/Border =. +? ['| \ "]/I" => '',
"/Face =. +? ['| \ "]/I" => '',
"/<Br. *?> [] */I "=>" </p> <p> ",
"/<Iframe. *?>. * <\/Iframe>/I "=> '',
"// I" => '', // Replace the space
"/<P. *?> [| \ X {3000} | \ r \ n] */ui "=> '<p>', // replace halfwidth, fullwidth spaces, line breaks, eliminate encoding issues when writing data to the database

);
$ Config = array (
// 'Indent '=> TRUE, // whether to indent
'Output-html '=> TRUE, // whether the output xhtml
'Show-body-only' => TRUE, // whether to obtain only the body
'Wrapp' => 0
);
$ Content = tidy_repair_string ($ content, $ config, 'utf8'); // use the tidy class library that comes with php to fix html tags. Otherwise, various strange situations may occur during replacement.
$ Content = trim ($ content );
Foreach ($ replaces as $ k => $ v ){
$ Content = preg_replace ($ k, $ v, $ content );
}

If (strpos ($ content, '<p>')> 6) // some content may start with missing <p> tag
$ Content = '<p>'. $ content;

$ Content = tidy_repair_string ($ content, $ config, 'utf8'); // you can fix it again to Remove empty html tags.
$ Content = trim ($ content );
Return $ content;
}

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.