Advertisement
terorama

php_examples

Sep 29th, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.22 KB | None | 0 0
  1. <?php
  2.    define ('TMPS','tmp_files');
  3.  
  4. echo '<br>';
  5. echo $_SERVER["SCRIPT_NAME"].'<br/>';
  6. echo $_SERVER["SCRIPT_FILENAME"].'<br/>';
  7. echo $_SERVER["PHP_SELF"].'<br/>';
  8. echo $_SERVER["REQUEST_URI"].'<br/>';
  9. echo $_SERVER["DOCUMENT_ROOT"].'<br/>';
  10. echo $_SERVER["QUERY_STRING"].'<br/>';
  11.  
  12. $testarr = preg_split ('/\//i', $_SERVER["SCRIPT_FILENAME"]);
  13. //$testarr = explode ('/',$_SERVER["SCRIPT_FILENAME"]);
  14.  
  15.    echo "<ul>";
  16.    foreach ($testarr as $key=>$el) {
  17.       echo "<li>{$el}</li>";
  18.    }
  19.    echo "</ul>";
  20.  
  21.    
  22.    $turl = 'http://google.ru/search';
  23.  
  24.    $zz = array('q'=>'test', 'oq'=>'ั‚ะตัั‚', 'num'=>100, 'hl'=>'ru', 'newwindow'=>1);
  25.  
  26.    $qs = http_build_query($zz);
  27.    echo $qs.'<br/>';
  28.    if (!file_exists(TMPS))
  29.       mkdir(TMPS);
  30.  
  31.    $fname=TMPS.'/'.str_replace(array('/',':'),array('_',''), $turl).'.tmpz';
  32.    
  33.    if (file_exists($fname)) {
  34.  
  35.       echo "getting from file<br/>";
  36.       $fh = fopen ($fname,'r');  
  37.       $s = fread($fh, filesize($fname));
  38.       fclose($fh);
  39.       echo '<pre>'.htmlentities($s).'</pre>';
  40.    }
  41.    else {
  42.       echo "getting from url<br/>";
  43.       $s = file_get_contents($turl.'?'.$qs);
  44.       echo '<pre>'.htmlentities($s).'</pre>';
  45.       $fh = fopen($fname,'w');
  46.       fwrite($fh,$s, strlen($s));
  47.       fclose($fh);
  48.    }
  49.  
  50.    preg_match('/<[^>]*div[^>]*id=["\']search["\'][^>]*>/ims',$s, $matches, PREG_OFFSET_CAPTURE);
  51.    
  52.    
  53.  
  54.  // $s = substr ($s, $matches[1]);
  55.  
  56.    preg_match_all(
  57.    '/<\s*h3\s*class=["\']r["\']\s*>.*?<[^>]*a[^>]*href="(.*?)"[^>]*>(.*?)<\/a>/ims',
  58.    $s, $matches);
  59.  
  60.    for ($i=0; $i<count($matches[1]); $i++) {
  61.       $matches[1][$i]=substr(strstr($matches[1][$i],'url?q='),strlen('url?q='));
  62.       $matches[2][$i]=strip_tags($matches[2][$i]);
  63.    }
  64.    unset($matches[0]);
  65.  
  66.    //echo '<pre>'.print_r($matches[1], true).'</pre>';
  67.  
  68.    function parsearrurls($el) {
  69.  
  70.       $f = parse_url($el);
  71.  
  72.       $z = array();
  73.  
  74.       $z[0]=$el;
  75.       @$z[1]=$f['scheme'];
  76.       @$z[2]=$f['host'];
  77.       @$z[3]=$f['path'];
  78.       @$z[4]=$f['query'];
  79.       @$z[5]=$f['fragment'];
  80.  
  81.       return $z;
  82.    }
  83.    
  84.    $arrparts = array_map('parsearrurls', $matches[1]);
  85.    $func = create_function('$el','return $el[2];');
  86.  
  87.    $arrhosts = array_map($func, $arrparts);
  88.    foreach ($arrhosts as &$arrhost) {
  89.       //echo $arrhost.'<br/>';
  90.       $arrhost=substr(strstr($arrhost,'www.'),strlen('www.'));
  91.      
  92.    }
  93.    $arrhosts = array_unique($arrhosts,SORT_STRING);
  94.    
  95.    echo '<pre>'.print_r($arrhosts, true).'</pre>';
  96.    
  97. //----------------------------------------------------------------------------------
  98. ?>
  99.  
  100. <?php
  101.  
  102.    //-----------------------------
  103.   function fsort($a, $b) {
  104.  
  105.      if (is_dir($a)) {
  106.         if (is_dir($b)) {
  107.            return strcasecmp($a,$b);
  108.         }
  109.         else return -1;
  110.      } else {
  111.        if (is_dir($b))
  112.           return 1;
  113.        else
  114.           return strcasecmp($a,$b);
  115.          
  116.      }
  117.  
  118.   }
  119.   //-----------------------------
  120.   function rpart($a) {
  121.      if (is_dir($a))
  122.         return '<a href="#" onclick="opclo(this);return false;"><b>'.
  123.                        substr(strrchr($a,'/'),1).'</b></a>';
  124.      else
  125.         return substr(strrchr($a,'/'),1);
  126.   }
  127.  
  128.    //-----------------------------
  129.   function printarel ($el, $key) {
  130.      echo $el.'<br>';
  131.      if (is_dir($key)) {
  132.         $f = array();
  133.         echo '<div class="dirdiv">';
  134.         zreaddir($key, $f);
  135.         echo '</div>';
  136.      }
  137.   }
  138.  
  139.   //-----------------------------
  140.   function zreaddir($dirstr, &$inarr) {
  141.  
  142.    
  143.      $d = opendir($dirstr);
  144.      
  145.      while ($f=readdir($d)) {
  146.         if (($f=='.') || ($f=='..')) continue;
  147.            $inarr[$dirstr.'/'.$f]=$dirstr.'/'.$f;
  148.        
  149.      }
  150.  
  151.      uasort($inarr,'fsort');
  152.      $inarr = array_map('rpart',$inarr);
  153.  
  154.      echo '<div style="margin-left:40px;">';
  155.         array_walk($inarr,'printarel');
  156.      echo '</div>';
  157.   }
  158.  
  159.    $uuu = <<<INF
  160.  
  161.    <script type="text/javascript">
  162.       function opclo(el) {
  163.          
  164.            z = el.nextSibling;
  165.            while (z) {
  166.             if (z.className=='dirdiv') {
  167.                z.style.display=(!z.style.display)?'block':'';
  168.                break;
  169.                }
  170.             z=z.nextSibling;
  171.          }
  172.       }
  173.    </script>
  174.  
  175.    <style type="text/css">
  176.       a {
  177.          color:black;
  178.          text-decoration:none;
  179.       }
  180.       .dirdiv {
  181.          display:none;
  182.       }
  183.    </style>
  184. INF;
  185.  
  186.    echo $uuu;
  187.    $basearr = array();
  188.    zreaddir('..', $basearr);
  189. //-------------------------------------------------------------------------------
  190. ?>
  191.  
  192. <?php
  193.    
  194.    $fh = fopen ('urls.txt','r');
  195.    $s= fread ($fh, filesize('urls.txt'));
  196.  
  197.    $arr = explode('|', $s);
  198.  
  199.    //-------------------------------------
  200.    function z_el($el) {
  201.       echo $el."<br/>";
  202.    }
  203.    //-------------------------------------
  204.    function filt($el) {
  205.       return $el!='';
  206.    }
  207.    //-------------------------------------
  208.    function getdom($el) {
  209.       return strrchr($el,'.');
  210.    }
  211.    //-------------------------------------
  212.    function combarrs($el1, $el4) {
  213.  
  214.       $f= array();
  215.       $f['domain']=$el4;
  216.       $f['addr']=$el1;
  217.  
  218.       return $f;
  219.    }
  220.    //-------------------------------------
  221.    function zsort($a, $b) {
  222.  
  223.       $uu = strcasecmp($a['domain'], $b['domain']);
  224.       if ($uu==0)
  225.          $uu = strcasecmp($a['addr'], $b['addr']);
  226.  
  227.       return $uu;
  228.    }
  229.    //-------------------------------------
  230.    function restruct($arr) {
  231.      
  232.        $newarr = array();
  233.        
  234.        foreach ($arr as $el) {
  235.  
  236.           if (!array_key_exists($el['domain'],$newarr)) {
  237.              $newarr[$el['domain']]=array();
  238.           }
  239.           $newarr[$el['domain']][]= $el['addr'];
  240.        }
  241.  
  242.        return $newarr;
  243.        
  244.    }
  245.    //-------------------------------------
  246.  
  247.    $func = create_function('$el','echo $el."<br/>";');
  248.  
  249.    $arr = array_filter($arr, 'filt');
  250.    $arrdoms = array_map('getdom',$arr);
  251.  
  252.    $arrcomb = array_map('combarrs', $arr, $arrdoms);
  253.  
  254.    usort ($arrcomb,'zsort');
  255.  
  256.    $arrcomb4 = restruct($arrcomb);
  257.  
  258.    echo '<pre>'.print_r($arrcomb4,true).'</pre>';
  259.    //array_walk($arrdoms, $func);
  260.    
  261.  
  262.  
  263. //-------------------------------------------------------------------------------------
  264. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement