Функции парсинга web-страниц

Тема в разделе "Регулярные выражения", создана пользователем Jensi, 1 июн 2009.

  1. Jensi

    Jensi Активный пользователь

    С нами с:
    9 апр 2009
    Сообщения:
    299
    Симпатии:
    0
    Код:
    <?php 
    
    // получение doctype документа
    function get_doctype($file){ 
        $h1tags = preg_match('/<!DOCTYPE (\w.*)dtd">/is',$file,$patterns); 
        $res = array(); 
        array_push($res,$patterns[0]); 
        array_push($res,count($patterns[0])); 
        return $res; 
    } 
    
    // получение заголовка страницы
    function get_doc_title($file){ 
        $h1tags = preg_match('/<title> ?.* <\/title>/isx',$file,$patterns); 
        $res = array(); 
        array_push($res,$patterns[0]); 
        array_push($res,count($patterns[0])); 
        return $res; 
    } 
    
    // получение ключевых слов
    function get_keywords($file){ 
        $h1tags = preg_match('/(<meta name="keywords" content="(.*)" \/>)/i',$file,$patterns); 
        $res = array(); 
        array_push($res,$patterns[2]); 
        array_push($res,count($patterns[2])); 
        return $res; 
    } 
    
    // получение rel ссылок из заголовков сайта 
    function get_link_rel($file){ 
        $h1tags = preg_match_all('/(rel=)(".*") href=(".*")/im',$file,$patterns); 
        $res = array(); 
        array_push($res,$patterns); 
        array_push($res,count($patterns[2])); 
        return $res; 
    } 
    // получение внешних css
    function get_external_css($file){ 
        $h1tags = preg_match_all('/(href=")(\w.*\.css)"/i',$file,$patterns); 
        $res = array(); 
        array_push($res,$patterns[2]); 
        array_push($res,count($patterns[2])); 
        return $res; 
    } 
    
    // получение всех h1 тэгов 
    function get_h1($file){ 
        $h1tags = preg_match_all("/(<h1.*>)(\w.*)(<\/h1>)/isxmU",$file,$patterns); 
        $res = array(); 
        array_push($res,$patterns[2]); 
        array_push($res,count($patterns[2])); 
        return $res; 
    } 
    
    // получение всех h2 тэгов  
        function get_h2($file){ 
        $h1tags = preg_match_all("/(<h2.*>)(\w.*)(<\/h2>)/isxmU",$file,$patterns); 
        $res = array(); 
        array_push($res,$patterns[2]); 
        array_push($res,count($patterns[2])); 
        return $res; 
    } 
    
    // получение всех h3 tags 
    function get_h3($file){ 
        $h1tags = preg_match_all("/(<h3.*>)(\w.*)(<\/h3>)/ismU",$file,$patterns); 
        $res = array(); 
        array_push($res,$patterns[2]); 
        array_push($res,count($patterns[2])); 
        return $res; 
    } 
    
    // получение всех h4 tags 
    function get_h4($file){ 
        $h1tags = preg_match_all("/(<h4.*>)(\w.*)(<\/h4>)/ismU",$file,$patterns); 
        $res = array(); 
        array_push($res,$patterns[2]); 
        array_push($res,count($patterns[2])); 
        return $res; 
    } 
    
    // получение всех h5 tags 
    function get_h5($file){ 
        $h1tags = preg_match_all("/(<h5.*>)(\w.*)(<\/h5>)/ismU",$file,$patterns); 
        $res = array(); 
        array_push($res,$patterns[2]); 
        array_push($res,count($patterns[2])); 
        return $res; 
    } 
    
    // получение всех h6 tags 
    function get_h6($file){ 
        $h1tags = preg_match_all("/(<h6.*>)(\w.*)(<\/h6>)/ismU",$file,$patterns); 
        $res = array(); 
        array_push($res,$patterns[2]); 
        array_push($res,count($patterns[2])); 
        return $res; 
    } 
    
    // получение содержания всех p тэгов 
    function get_p($file){ 
        $h1tags = preg_match_all("/(<p.*>)(\w.*)(<\/p>)/ismU",$file,$patterns); 
        $res = array(); 
        array_push($res,$patterns[2]); 
        array_push($res,count($patterns[2])); 
        return $res; 
    } 
    
    // получение всех названий ссылок 
    function get_a_content($file){ 
        $h1count = preg_match_all("/(<a.*>)(\w.*)(<.*>)/ismU",$file,$patterns); 
        return $patterns[2]; 
    } 
    
    // получение всех урликов 
    function get_a_href($file){ 
        $h1count = preg_match_all('/(href=")))*???")/i',$file,$patterns); 
        return $patterns[2]; 
    } 
    
    // подсчет кол-ва href 
    function get_a_href_count($file){ 
        $h1count = preg_match_all('/<(a.*) href=\"(.*?)\"(.*)<\/a>/',$file,$patterns); 
        return count($patterns[0]); 
    } 
    
    // получение всех доп тегов внутри тега ссылки
    function get_a_additionaltags($file){ 
        $h1count = preg_match_all('/<(a.*) href="(.*?)"(.*)>(.*)(<\/a>)/',$file,$patterns); 
        return $patterns[3]; 
    } 
    
    // получение всех  span ов 
    function get_span($file){ 
        $h1count = preg_match_all('/(<span .*>)))*)(<\/span>)/',$file,$patterns); 
        $res = array(); 
        array_push($res,$patterns[2]); 
        array_push($res,count($patterns[2])); 
        return $res; 
    } 
    
    // получение всех сркриптов - Яваскрипт в чатсности
    function get_script($file){ 
        $h1count = preg_match_all('/(<script.*>)))*)(<\/script>)/imxsU',$file,$patterns); 
        $res = array(); 
        array_push($res,$patterns[2]); 
        array_push($res,count($patterns[2])); 
        return $res; 
    } 
    
    // получение всех ul
    function get_ul($file){ 
        $h1count = preg_match_all('/(<ul \w*>)))*)(<\/ul>)/ismxU',$file,$patterns); 
        $res = array(); 
        array_push($res,$patterns[2]); 
        array_push($res,count($patterns[2])); 
        return $res; 
    } 
    
    // получение всех li
    function get_li($file){ 
        $h1count = preg_match_all('/(<li \w*>)))*)(<\/li>)/ismxU',$file,$patterns); 
        $res = array(); 
        array_push($res,$patterns[2]); 
        array_push($res,count($patterns[2])); 
        return $res; 
    } 
    
    // получение всех комментов на странице 
    function get_comments($file){ 
        $h1count = preg_match_all('/(<!--)))*)(-->)/isU',$file,$patterns); 
        $res = array(); 
        array_push($res,$patterns[2]); 
        array_push($res,count($patterns[2])); 
        return $res; 
    } 
    
    // получение всех ID используемых на странице 
    function get_ids($file){ 
        $h1count = preg_match_all('/(id="(\w*)")/is',$file,$patterns); 
        $res = array(); 
        array_push($res,$patterns[2]); 
        array_push($res,count($patterns[2])); 
        return $res; 
    } 
    // получение всех классов документа 
    function get_classes($file){ 
        $h1count = preg_match_all('/(class="(\w*)")/is',$file,$patterns); 
        $res = array(); 
        array_push($res,$patterns[2]); 
        array_push($res,count($patterns[2])); 
        return $res; 
    } 
    
    // получение всех мета тег данных
    function get_meta_content($file){ 
        $h1count = preg_match_all('/(<meta)))*="(.*)").\/>/ix',$file,$patterns); 
        $res = array(); 
        array_push($res,$patterns[2]); 
        array_push($res,count($patterns[2])); 
        return $res; 
    } 
    
    // получение всех стилей (указанных построчно)
    function get_styles($file){ 
        $h1count = preg_match_all('/(style=")))*???")/is',$file,$patterns); 
        $res = array(); 
        array_push($res,$patterns[2]); 
        array_push($res,count($patterns[2])); 
        return $res; 
    } 
    
    // получение всех титлов тегов
    function get_tag_titles($file){ 
        $h1count = preg_match_all('/(title=)"(.*)"(.*)/',$file,$patterns); 
        $res = array(); 
        array_push($res,$patterns[2]); 
        array_push($res,count($patterns[2])); 
        return $res; 
    } 
    
    // получение всех альтернативных названий картинок
    function get_image_alt($file){ 
        $h1count = preg_match_all('/(alt=...[a-zA-Z0-9\s]{1,})/',$file,$patterns); 
        $res = array(); 
        array_push($res,$patterns[2]); 
        array_push($res,count($patterns[2])); 
        return $res; 
    } 
    
    // получение всех изображений
    function get_images($file){ 
        $h1count = preg_match_all('/(<img)\s (src="([a-zA-Z0-9\.;:\/\?&=_|\r|\n]{1,})")/isxmU',$file,$patterns); 
        $res = array(); 
        array_push($res,$patterns[3]); 
        array_push($res,count($patterns[3])); 
        return $res; 
    } 
    
    // получение всех имейлов с тегом mailto:
    function get_mailto($file){ 
        $h1count = preg_match_all('/(<a\shref=")(mailto:)([a-zA-Z@0-9\.]{1,})"/ims',$file,$patterns); 
        $res = array(); 
        array_push($res,$patterns[3]); 
        array_push($res,count($patterns[3])); 
        return $res; 
    } 
    
    // получение всех имейлов
    function get_emails($file){ 
        $h1count = preg_match_all('/[a-zA-Z0-9_-]{1,}@[a-zA-Z0-9-_]{1,}\.[a-zA-Z]{1,4}/',$file,$patterns); 
        $res = array(); 
        array_push($res,$patterns[0]); 
        array_push($res,count($patterns[0])); 
        return $res; 
    } 
    
    // подсчет используемых ключевых слов
    function countkeyword($word,$file){ 
        $x = preg_match_all("/(.*)($word)))*)/",$file,$patterns); 
        return count($patterns); 
    } 
    
    // получение всех внутренных ссылок сайта
    function get_internal_links($array){ 
        $result = array(); 
        $count = count($array); 
            for($i=0;$i<$count;$i++){ 
                if(!empty($array[$i])){        
                    if(strpos($array[$i],"www",0) === false){ 
                        if(strpos($array[$i],"http",0) === false){                    
                            array_push($result,$array[$i]); 
                        } 
                    } 
                } 
            } 
        return $result; 
    } 
    
    // получение всех внешних ссылок сайта
    function get_external_links($array){ 
        $result = array(); 
        $count = count($array); 
            for($i=0;$i<$count;$i++){ 
                if(!empty($array[$i])){        
                    if(strpos($array[$i],"www",0) !== false){ 
                        if(strpos($array[$i],"http",0) !== false){                    
                            array_push($result,$array[$i]); 
                        } 
                    } 
                } 
            } 
        return $result; 
    } 
    
    // получение главного урла сайта
    function get_main_url($url){ 
        $parts = parse_url($url); 
        $url = $parts["scheme"] ."://".$parts["host"]; 
        return $url; 
    } 
    
    // получение всех доменного имени без ввв и хвоста 
    function get_domain_name_only($url){ 
        $match = preg_match("/(.*:\/\/)\w{0,}(.*)\...*)/",$url,$patterns); 
        $patterns[2] = str_replace(".","",$patterns[2]); 
        return $patterns[2]; 
    } 
    
    //Выдирание всех форм из документа
    function getforms($document)  {    
        $elements = array();
    
        preg_match_all("'<\/?(FORM|INPUT|SELECT|TEXTAREA|(OPTION))[^<>]*>(((2)))*(?=<\/?(option|select)[^<>]*>[\r\n]*)|(?=[\r\n]*))|(?=[\r\n]*))'Usi",$document,$elements);
            
        $match = implode("\r\n",$elements[0]);
        return $match;
    } 
    ?> 
    Ну может кому пригодится это всё:) Простите,если где-то уже есть такая тема О_о
     
  2. Mr.M.I.T.

    Mr.M.I.T. Старожил

    С нами с:
    28 янв 2008
    Сообщения:
    4.581
    Симпатии:
    1
    Адрес:
    у тебя канфетка?