2

I'm using a wordpress plugin called Movie Poster which grabs a movie's info from imdb and displays it in a post. How do I grab just the first Production Co before the first comma? I'm confused on how this code works. Here's the code which grabs all the production co:

$arr['productions'] = array();
foreach($this->match('/<a.*?>(.*?)<\/a>/ms', 
           $this->match('/Production Co.?:(.*?)(<\/div>|See more)/ms', $html, 1)
        , 1) as $m)
    array_push($arr['productions'], $m);`

Thanks for your time

4
  • 4
    you shouldn't be parsing html with regexes. that's why we have php.net/dom Commented Apr 30, 2013 at 17:06
  • Thanks for the fast reply. What could I do to fix this? Commented Apr 30, 2013 at 17:08
  • @user2336811: Contact the developer of that plugin, tell him about your issue and negotiate a fix. Commented Apr 30, 2013 at 17:15
  • I've been trying for weeks but no response :( Commented Apr 30, 2013 at 17:17

1 Answer 1

1

So you just want one Production Co's info?

Why not just get rid of the foreach? You currently have a loop going through and looking for all Production Co. and returning.

Or you could change it to a While statement and add a counter so that it only passes through once.

$arr['productions'] = array();
$i = 0;
while($i < 1) {
   $m = $this->match('/<a.*?>(.*?)<\/a>/ms', 
       $this->match('/Production Co.?:(.*?)(<\/div>|See more)/ms', $html, 1)
    , 1);
   array_push($arr['productions'], $m);
   $i++;
}

Not sure if the syntax is exactly correct, but should get you in the right direction. Keep in mind, I'm not familiar with the plugin or if I've understood exactly what you're trying to accomplish.

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

7 Comments

Thanks for your answer but I'm getting this error "Parse error: syntax error, unexpected T_AS"
I'm getting "Fatal error: Maximum execution time of 30 seconds exceeded" in this line of code: function match($regex, $str, $i = 0){ if(preg_match($regex, $str, $match) == 1) return $match[$i]; else return false; }
Cool, I didnt get any errors but the post only displays a comma.
In your original code snippet, you have a ` at the end of the ;. Is that needed? Wasn't sure if that was just a typo, etc.
The problem may be that we're not assigning $m anywhere. Try changing it to $m = $this->match...
|

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.