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;
}