Web爬网程序的内爆错误

I am on the final strip for a web crawler that I am writing.

The web crawler crawls BBC News and then inserts links into the database along with titles and descriptions etc. all that works but I have an array with all the starting urls, so that only links that begin with any of them are only inserted.

I am using a foreach to loop over all the array variables for the array of all links and checking if they match the criteria, inserting into the new array and then imploding that to a string and then inserting into a mysql database.

An error appears, however, regarding my implode function. I am stuck.

    $bbc_values = array('http://www.bbc.co.uk/news/health-', 'http://www.bbc.co.uk/news/politics-', 'http://www.bbc.co.uk/news/uk-', 'http://www.bbc.co.uk/news/technology-', 'http://www.bbc.co.uk/news/world-', 'http://www.bbc.co.uk/news/england-', 'http://www.bbc.co.uk/news/northern_ireland-', 'http://www.bbc.co.uk/news/scotland-', 'http://www.bbc.co.uk/news/wales-', 'http://www.bbc.co.uk/news/business-', 'http://www.bbc.co.uk/news/education-', 'http://www.bbc.co.uk/news/science_and_enviroment-', 'http://www.bbc.co.uk/news/entertainment_and_arts-', 'http://edition.cnn.com/');


  foreach ($links as $link) {
  $output = array(
"title"       => Titles($link), //dont know what Titles is, variable or string?
"description" => getMetas($link),
"keywords" => getKeywords($link), 
"link"        => $link                 
 );
if (empty($output["description"])) {
$output["description"] = getWord($link);
}

    foreach ($output as $new_array) {
if (in_array($new_array['link'], $bbc_values)) {
    $news_stories[] = $new_array;
}
     }



 $data = '"' . implode('" , "', $news_stories) . '"';
 $result = mysql_query("INSERT INTO news_story (`title`, `description`, `keywords`, `link`) VALUES (" . $data . ")");

Firstly, $links is not defined. Did you mean $bbc_value ?

Otherwise, you have to close the first foreach (closing } missing)

Inside your foreach loop you have

$news_stories[] = $new_array;

which will produce an array of arrays maybe something like following

array(
    array(
        'title'=>'title1',
        'description'=>'description1',
        'keywords'=>'keywords1',
        'link'=>'link1'
    ),
    array(
        'title'=>'title2',
        'description'=>'description2',
        'keywords'=>'keywords2',
        'link'=>'link2'
    )
);

and you are using implode outside of loop like this

$data = '"' . implode('" , "', $news_stories) . '"';

which should not work unless you specify an index in the array. So, if you use following code

$data='"' . implode('" , "', $news_stories[0]) . '"';
echo $data;

then it'll implode first array item from the $news_stories array and it'll produce following

"title1" , "description1" , "keywords1" , "link1"

If you want to produce following

$result = mysql_query("INSERT INTO news_story (`title`, `description`, `keywords`, `link`) VALUES ('title1' , 'description1' , 'keywords1' , 'link1')");

then you can use

$data="'" . implode("' , '", $news_stories[0]) . "'";

so if you write

$result = mysql_query("INSERT INTO news_story (`title`, `description`, `keywords`, `link`) VALUES (" . $data . ")");

then it'll out produce

$result = mysql_query("INSERT INTO news_story (`title`, `description`, `keywords`, `link`) VALUES ('title1' , 'description1' , 'keywords1' , 'link1')");