1

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!

1
  • You don't need a semicolon after the function's closing curly brace. It looks like your foreach loop needs to actually load the page contents from the URL in $html before performing its other actions. Commented Aug 2, 2010 at 21:51

3 Answers 3

5

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

?>
Sign up to request clarification or add additional context in comments.

1 Comment

Oh my god - im so stupid! proofe its too late for me i thought of all but not that i forgot to put this specific line in...... thx!
0

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

Comments

0

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.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.