1

I'm using preg_match_all to import images using the

Currently works:

 //get image url from url
   preg_match_all('/<img[^>]+>/i',$file->body, $images);

I tried this:

 //get image url from url
   preg_match_all('/<img id="charity"[^>]+>/i',$file->body, $images);

Once altered I get a 500 server error so I assume my syntax is wrong. How do I correctly alter this to work correctly?

Rocco The Taco
  • 3,695
  • 13
  • 46
  • 79
  • 2
    Also, enable `error_reporting()` when developing, and check your error logs. – Madara's Ghost Aug 24 '13 at 12:29
  • 1
    This is not a duplicate. – Rocco The Taco Aug 24 '13 at 12:37
  • 1
    When you'll completely understand the answer given on that question, you'll understand why it is. – Madara's Ghost Aug 24 '13 at 12:40
  • It's a good reference but totally bloated. Current preg_match_all questions are very weak and go unanswered. This question helps illustrate how to correctly use it. If you don't know the answer it does not mean you should try to mark as duplicate and close it. – Rocco The Taco Aug 24 '13 at 12:42
  • @RoccoTheTaco: I know how to answer. I'm just tired answering those HTML regex questions over and over again when the alternative should be as clear as the sun. – Madara's Ghost Aug 24 '13 at 12:46
  • Try using regex checkers, here are a few: http://regex101.com/, http://regexpal.com/, http://www.quanetic.com/Regex. Once you know your regex is sound you can start checking for errors elsewhere. – Technoh Aug 24 '13 at 13:25
  • @MadaraUchiha ...nothing personal ... do a search on preg_match_all and you'll find unanswered questions and weak answers. – Rocco The Taco Aug 24 '13 at 13:36
  • You're far better off using DOMDocument/DOMXPath for this, it's the proper tool for the job. It's almost impossible to reliably parse HTML with regex – GordonM Aug 24 '13 at 14:48

1 Answers1

0

That regexp seems to be OK. Your error must be anywere else.

You can test it with something like:

<?php
$test='
<img src="http://example.com/image.jpg"/>
foo bar
<img id="charity" src="local/image.jpg"/>
';
preg_match_all('/<img[^>]+>/i',$test, $images);
print_r($images);
preg_match_all('/<img id="charity"[^>]+>/i',$test, $images);
print_r($images);

Output:

Array
(
    [0] => Array
        (
            [0] => <img src="http://example.com/image.jpg"/>
            [1] => <img id="charity" src="local/image.jpg"/>
        )

)
Array
(
    [0] => Array
        (
            [0] => <img id="charity" src="local/image.jpg"/>
        )

)

Tested with PHP 5.2.13.

isalgueiro
  • 1,973
  • 16
  • 20