0

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?

1
  • In regex bacslash is an operator to check for regex special chars. I suggest you to check regex101.com Commented Mar 26, 2015 at 17:29

1 Answer 1

2

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 (?:\"|')?.

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

Comments

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.