bdart
bdart

Reputation: 755

php regex how to backslash

I want made this regex and tried to use it within my PHP script. But it is just working in the reggae builder not in my script. I think it have to be something with the backslashes but I really placed and killed a lot of them without some effort.

Link to the working regex

This is the provided php version, but it simply won't work:

$re = "/< *img[^>]*title= *\\\\ *[\\\"\\']?([^\\\\\\\"\\']*)[^>]*src= *\\\\ *[\\\"\\']? *data:image\\/jpeg;base64,([^\\\\\\\"\\']*)/"; 
$str = "<img style=\"\" title=\"Dies ist der Bild Titel\" src=\"data:image/jpeg;base64,/9j/4AAQ\">Hier ist Text<img title=\"Hier ist ein anderer\" src=\"data:image/jpeg;base64,/9j/4ABQ\">"; 

preg_match_all($re, $str, $matches);

What I have to consider making it work?

Upvotes: 0

Views: 41

Answers (1)

bloodyKnuckles
bloodyKnuckles

Reputation: 12079

Using:

$re = "/< *img[^>]*title= *\\ *(?:\"|')?([^\"']*)[^>]*src= *\\ *(?:\"|')? *data:image\/jpeg;base64,([^\"']*)[^>]/"; 

print_r($matches) outputs:

Array
(
    [0] => Array
        (
            [0] => <img style="" title="Dies ist der Bild Titel" src="data:image/jpeg;base64,/9j/4AAQ"
            [1] => <img title="Hier ist ein anderer" src="data:image/jpeg;base64,/9j/4ABQ"
        )

    [1] => Array
        (
            [0] => Dies ist der Bild Titel
            [1] => Hier ist ein anderer
        )

    [2] => Array
        (
            [0] => /9j/4AAQ
            [1] => /9j/4ABQ
        )

)

I used fewer escapes and changed the quotes in square brackets thing you had goin' on [\\\"\\']? to non-capturing alternations (?:\"|')?.

Upvotes: 2

Related Questions