0

I have some text with <img> tags in is that I need to divvy up. It's in the format

<img.../> Text text text <img.../>text text text<img.../> text text text

I have my regex qworking in preg_match_all so that I get

Array
(
    [0] => Array
        (
            [0] => <img ... />
            [1] => <img ... />
            [2] => <img ... />
            [3] => <img ... />
        )

But it would be really nice if I could get

Array
(
    [0] => Array
        (
            [0] => <img ... />
            [1] => text text text 
            [2] => <img ... />
            [3] => text text text 
            [4] => <img ... />
            [5] => text text text 
        )

I've tried a few things but I really don't have a good understanding of PCREs. I don't want to use preg_split if I can avoid it because each of the images tags are different.

(I understand that a general HTML parser cannot be written with regular expressions, but in this case, I think this will work because the input data that I'm working is in the form I described. There aren't going to be any nested image tags that I'll need to worry about.)

PS I've tried /!<img.+>/, /!(<img.+>)/, and /(!(<img.+>))/ to get the non-matches, but it returns an empty array. I don't know a good way to debug regexes to know what I'm doing wrong.

4
  • 1
    Your reason for avoiding preg_split is unreasoned. Please elaborate. If you already can match the tags and their variations, then ... Yes, why?
    – mario
    Nov 20, 2012 at 23:14
  • It's just a bother to loop through the matches and run preg_split on the full text to wind it down to nothing. If I can do the work in the preg_match_all command, why bother writing the loop?
    – user151841
    Nov 20, 2012 at 23:19
  • What loop now? preg_split (with the right parameter) already breaks up the source into specifically the chunks you want.
    – mario
    Nov 20, 2012 at 23:23
  • @mario preg_split takes one string as an argument. I have multiple different strings that I need to split it by. I would have to run preg_split for each match I got from preg_match_all, whittling down the text each time.
    – user151841
    Nov 20, 2012 at 23:26

2 Answers 2

1

I don't know what your issue (or actual code for that matter) is, but:

$r = preg_split('#<img[^>]+>#', $source, 0, PREG_SPLIT_DELIM_CAPTURE);

results in:

Array
(
    [0] => <img.../>
    [1] =>  Text text text 
    [2] => <img.../>
    [3] => text text text
    [4] => <img.../>
    [5] =>  text text text
)

In place of a proper regex, you can keep using your fixed strings (I presume) with #<img1>|<img2>|<img3># of course.

1
  • Ugh! Long day at work, was thinking "hard-coded string" and not "regular expression" :P Public embarrassment ensues...
    – user151841
    Nov 21, 2012 at 1:09
0

You can have the information you want just not quite in the right format by doing this:

preg_match_all('~(<img[^>]*>)([^<]+)~', $str, $matches);

//if inside your "text text text" areas you have other html tags, use this:
preg_match_all('~(<img[^>]*>)(.+?)(?=<img|$)~', $str, $matches);

At this point, $matches[0] contains the entire matched string. $matches[1] contains all of the matches from the first set of parenthesis and $matches[2] contains all of the matches from the second set of parenthesis.

Array ( 
  [0] => Array ( 
    [0] => <img.../> Text text text 
    [1] => <img.../>text text text 
    [2] => <img.../> text text text 
  )
  [1] => Array ( 
    [0] => <img.../> 
    [1] => <img.../> 
    [2] => <img.../> 
  ) 
  [2] => Array ( 
    [0] =>  Text text text 
    [1] => text text text 
    [2] =>  text text text 
  ) 
) 

Now if you really need it formatted the way you would like, just add these lines of code:

$answer = array();
foreach($matches[0] as $i=>$match){
  $answer[] = $matches[1][$i];
  $answer[] = $matches[2][$i];
};

$answer now looks like this:

Array ( 
  [0] => <img ... />
  [1] =>  Text text text 
  [2] => <img ... />
  [3] => text text text 
  [4] => <img ... />
  [5] =>  text text text 
)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.