开发者

Using Array as Argument for a Function

Code inside Function is working, but as i want to process more then one Url i wanted to make it a function using a array to get the urls to process. Here is the Code:

    <?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>&开发者_JAVA技巧lt;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);
?>

Where is my problem as the function stops working at the beginning of the foreach.... Thx in advance for your help!


I have a feeling you are missing a step in there... maybe a file_get_contents or the like? Right now you are running a bunch of string functions on the uri themselves, not the source at the uri.

Try this instead:

<?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);

?>


rather than pass an array why not loop through your array and pass each element to the function? - multiple calls to the same function...

judging by what you're function should you not be scraping the contents of eahc page rather than just the URL???


This line returns '' because there is no p tag in your URL strings..

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

What are you trying to do? If you're trying to get the content of those sites, use file_get_contents() or similar functions to fetch URL content.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜