views:

71

answers:

3

Hello!

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><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!

+5  A: 

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

?>
sberry2A
Oh my god - im so stupid!proofe its too late for mei thought of all but not that i forgot to put this specific line in......thx!
Arwed
A: 

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

ToonMariner
A: 

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.

babonk
the origin sites are real bad coded - but its free content they gave me to input into my site - so my intention was to remove all bad code except the raw text with some <p> tags. with the first answer i have seen i missed a line of code when i turned it into a function
Arwed