代码之家  ›  专栏  ›  技术社区  ›  Arwed

使用数组作为函数的参数

  •  1
  • Arwed  · 技术社区  · 15 年前

    函数中的代码正在工作,但由于我想处理多个Url,所以我想让它成为一个使用数组来获取要处理的Url的函数。代码如下:

        <?php
    $site = array("http://www.ihr-apotheker.de/cs1.html", "http://www.ihr-apotheker.de/cs2.html", "http://www.ihr-apotheker.de/cs3.html");
    
    function parseNAI($sites)
      {
      foreach ($sites as $html)
        {
          $clean_one = strstr($html, '<p>');
          $clean_one_class = str_replace('<p><span class="headline">', '<p class="headline gruen"><span>', $clean_one);
          $clean_one_class_a = strip_tags($clean_one_class, '<p><span><a>');
          $clean_one_class_b = preg_replace("/\s+/", " ", $clean_one_class_a);
          $str_one = preg_replace('#(<a.*>).*?(</a>)#', '$1$2', $clean_one_class_b);
          $ausgabe_one = strip_tags($str_one, '<p>');
          echo $ausgabe_one;
        } 
      };
    parseNAI($site);
    ?>
    

    3 回复  |  直到 15 年前
        1
  •  5
  •   Alex Jasmin    15 年前

    我有一种感觉,你在那里错过了一步。。。也许是一个 file_get_contents 或者类似的?现在,您正在uri本身上运行一组字符串函数,而不是uri上的源。

    请尝试以下操作:

    <?php
    $site = array("http://www.ihr-apotheker.de/cs1.html", "http://www.ihr-apotheker.de/cs2.html", "http://www.ihr-apotheker.de/cs3.html");
    
    function parseNAI($sites)
    {
        foreach ($sites as $url)
        {
            $html = file_get_contents($url);
            $clean_one = strstr($html, '<p>');
    
            $clean_one_class = str_replace('<p><span class="headline">', '<p class="headline gruen"><span>', $clean_one);
            $clean_one_class_a = strip_tags($clean_one_class, '<p><span><a>');
            $clean_one_class_b = preg_replace("/\s+/", " ", $clean_one_class_a);
            $str_one = preg_replace('#(<a.*>).*?(</a>)#', '$1$2', $clean_one_class_b);
            $ausgabe_one = strip_tags($str_one, '<p>');
            echo $ausgabe_one;
        } 
    };
    parseNAI($site);
    
    ?>
    
        2
  •  0
  •   Ian Wood    15 年前

    与其传递数组,为什么不循环遍历数组并将每个元素传递给函数对同一函数的多个调用。。。

        3
  •  0
  •   Ben G    15 年前

    此行返回“”,因为URL字符串中没有p标记。。

    $clean_one = strstr($html, '<p>');
    

    你想干什么?如果您试图获取这些站点的内容,请使用file\u get\u contents()或类似函数获取URL内容。

    推荐文章