【问题标题】:Using Array as Argument for a Function使用数组作为函数的参数
【发布时间】:2010-08-02 21:41:40
【问题描述】:

Function 中的代码正在运行,但由于我想处理多个 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);
?>

我的问题在哪里,因为函数在 foreach 开始时停止工作...... 提前感谢您的帮助!

【问题讨论】:

  • 函数的右花括号后不需要分号。看起来您的 foreach 循环需要在执行其他操作之前从$html 中的 URL 实际加载页面内容。

标签: php arrays function


【解决方案1】:

我感觉你错过了一个步骤......也许是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】:

而不是传递一个数组,为什么不循环遍历你的数组并将每个元素传递给函数呢? - 多次调用同一个函数...

根据您的功能判断,您是否应该不抓取 eahc 页面的内容而不仅仅是 URL ???

【讨论】:

    【解决方案3】:

    此行返回 '' 因为您的 URL 字符串中没有 p 标记..

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

    你想做什么?如果您尝试获取这些网站的内容,请使用 file_get_contents() 或类似函数来获取 URL 内容。

    【讨论】:

    • 原始站点的编码确实很糟糕-但是他们让我输入到我的站点中的免费内容-所以我的意图是删除所有错误代码,除了带有一些

      标签的原始文本。在我看到的第一个答案中,当我把它变成一个函数时,我错过了一行代码

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    • 1970-01-01
    • 1970-01-01
    • 2015-12-24
    相关资源
    最近更新 更多