Advertisement
slo_nik

Untitled

Jul 13th, 2016
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.98 KB | None | 0 0
  1.         $ch = curl_init();
  2.         curl_setopt($ch, CURLOPT_URL, $address);
  3.         curl_setopt($ch, CURLOPT_HEADER, 1);
  4.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  5.         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  6.         curl_setopt($ch, CURLOPT_USERAGENT,
  7.                     'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/34.0.1847.116 Chrome/34.0.1847.116 Safari/537.36');
  8.  
  9.         $dirty_html = curl_exec($ch);
  10.  
  11.         $response = (object)curl_getinfo($ch);
  12.  
  13.         if(stripos($response->content_type, 'windows-1251')){;
  14.           $dirty_html = mb_convert_encoding($dirty_html, 'utf-8', 'cp1251');
  15.         }
  16.         curl_close($ch);
  17.  
  18.         $prf = new HTMLPurifier();
  19.         $clean_html = $prf->process($dirty_html,[
  20.               'AutoFormat.AutoParagraph' => true,
  21.               'AutoFormat.RemoveEmpty' => true,
  22.               'AutoFormat.RemoveEmpty.RemoveNbsp' => true,
  23.               'HTML.AllowedElements' => ['p','a','br','table','tbody','tr','th','td','h1','span','big'],
  24.               'HTML.ForbiddenAttributes' => [
  25.                                              'a@href',
  26.                                              'a@style',
  27.                                              'a@rel',
  28.                                              'a@class',
  29.                                              'a@title',
  30.                                              'p@class',
  31.                                              'p@style',
  32.                                              'table@style',
  33.                                              'table@border',
  34.                                              'table@width',
  35.                                              'td@class',
  36.                                              'th@class',
  37.                                              'td@valign',
  38.                                              'td@style',
  39.                                              'td@width',
  40.                                              'td@colspan',
  41.                                              'td@align',
  42.                                              'td@rowspan',
  43.                                              'span@class'
  44.                                             ]
  45.           ]);
  46.   $search = ['<p>','</p>','&amp;','<a>','</a>','<table>','<tbody>','</tbody>','</table>','<tr>','</tr>','<th>','</th>','<td>','</td>','<h1>','</h1>','<span>','</span>','<big>','</big>'];
  47.         $replace = ['',"\r\n",'','',"\r\n",'','','','','',"\r\n",'',' ','','','',"\r\n",'',' ','',' '];
  48.         $clean_html = preg_replace("#(?:<p>\s+</p>)#", '', $clean_html);
  49.         $clean_html = str_replace(["\r","\n","\r\n","\t",'  '], '', $clean_html);
  50.         $clean_html = str_replace($search, $replace, $clean_html);
  51.         $clean_html = preg_replace("#(?:\r\n?|\n){2,}#", "\r\n", $clean_html);
  52.         $clean_html = str_replace(['<br>', '<br />'], " ", $clean_html);
  53.         $clean_html = preg_replace("#(?:\"\'\+;)+#", '', $clean_html);
  54.  
  55.         echo $clean_html
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement