1

at my current website i am using this code to get the first image , witch is inside the post

$first_img = '';
    $my1content = AD($row['post_content']);
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $my1content, $matches); 
    $first_img = $matches [1] [0];
    if(empty($first_img)){ //Defines a default image
        $first_img = "/img/default.png";
    }

I wanted to know how to get all the images that are in the post, not only the first one. Thank you for reading this message.

Meo
  • 241
  • 5
  • 11
  • 1
    possible duplicate of [Best methods to parse HTML with PHP](http://stackoverflow.com/questions/3577641/best-methods-to-parse-html-with-php) – ajreal Sep 02 '11 at 22:02
  • possible duplicate of [Grabbing the href attribute of an A element](http://stackoverflow.com/questions/3820666/grabbing-the-href-attribute-of-an-a-element) – Gordon Sep 02 '11 at 22:28

1 Answers1

1

$matches[1] should be an array, iterate over $matches[1] to get all the img tags. This assumes that $my1content has all the content.

for ($matches[1] as $match) {
    //do the stuff you want to do with a match
    $imgUrl = $match[1]; //Do something with this
}
Myles
  • 20,860
  • 4
  • 28
  • 37
  • i need to get a variable like $first_image, how can i get it with your code ? – Meo Sep 02 '11 at 22:13
  • $match[1] should be capturing that, based on your regex. You ought to investigate the documentation for print_r() and/or var_dump(), so you can verify these statements in a debug message. – Myles Sep 02 '11 at 22:14
  • please can you update your code, and based on my code write how it should be please. thank you – Meo Sep 02 '11 at 22:18
  • I can't as you don't explain in your code whether you want to store this is a data structure or print out or what you want to do with the list of img src attributes you are getting. The best I can do is give you the means and let you figure out how to solve the problem you have been presented. – Myles Sep 02 '11 at 22:21
  • sorry i just want to use echo and pring the post images, that is what i need to do . as i said, the images are all inside the post with their tags. – Meo Sep 02 '11 at 22:23
  • i have paste this down my current code and iget this error... Parse error: syntax error, unexpected T_AS, expecting ';' in /home/xxx/public_html/3/core.php on line 1761 , i am doing smth wrong ? – Meo Sep 02 '11 at 22:29
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3114/discussion-between-myles-and-meo) – Myles Sep 02 '11 at 22:56
  • Ok, i am waiting for you in chat – Meo Sep 02 '11 at 23:33