English 中文(简体)
IMDB Grabber PHP
原标题:

I am recieving an error:

Notice: Undefined variable: content in C:wampwwwincludesimdbgrabber.php on line 17

When using this code:

<?php
//url
$url =  http://www.imdb.com/title/tt0367882/ ;

//get the page content
$imdb_content = get_data($url);

//parse for product name
$name = get_match( /<title>(.*)</title>/isU ,$imdb_content);
$director = strip_tags(get_match( /<h5[^>]*>Director:</h5>(.*)</div>/isU ,$imdb_content));
$plot = get_match( /<h5[^>]*>Plot:</h5>(.*)</div>/isU ,$imdb_content);
$release_date = get_match( /<h5[^>]*>Release Date:</h5>(.*)</div>/isU ,$imdb_content);
$mpaa = get_match( /<a href="/mpaa">MPAA</a>:</h5>(.*)</div>/isU ,$imdb_content);
$run_time = get_match( /Runtime:</h5>(.*)</div>/isU ,$imdb_content);

//build content


line 17 -->  $content.=  <h2>Film</h2><p> .$name. </p> ;
    $content.=  <h2>Director</h2><p> .$director. </p> ;
    $content.=  <h2>Plot</h2><p> .substr($plot,0,strpos($plot, <a )). </p> ;
    $content.=  <h2>Release Date</h2><p> .substr($release_date,0,strpos($release_date, <a )). </p> ;
    $content.=  <h2>MPAA</h2><p> .$mpaa. </p> ;
    $content.=  <h2>Run Time</h2><p> .$run_time. </p> ;
    $content.=  <h2>Full Details</h2><p><a href=" .$url. " rel="nofollow"> .$url. </a></p> ;

    echo $content;

//gets the match content
function get_match($regex,$content)
{
    preg_match($regex,$content,$matches);
    return $matches[1];
}

//gets the data from a URL
function get_data($url)
{
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
?>
最佳回答

You are appending content to a variable that doesn t exist. Change line 17 to an assignment:

$content =  <h2>Film</h2><p> .$name. </p> ;

You could also change that section of code to the following, which is slightly neater:

$content =  <h2>Film</h2><p> .$name. </p> 
         .  <h2>Director</h2><p> .$director. </p> 
         .  <h2>Plot</h2><p> .substr($plot,0,strpos($plot, <a )). </p> 
      // etc
问题回答

You are trying to add something to the variable $content when it doesn t exist yet, this naturally triggers an error.

Try replacing $content.= with $content= in line 17.

You re not receiving an error, you re receiving a notice because you try to concatenate something to a variable that does not exist. Remove the dot from .= at line 17 or put $content = before line 17.

Apart from what others have said there is another issue with your code that needs attention. You are not checking the return value of preg_match before you return the value from the function get_match. You should do something like:

if(preg_match($regex,$content,$matches))
  return $matches[1];
else
  // return some default




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签