Regex help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Minouch
Forum Newbie
Posts: 9
Joined: Thu Sep 12, 2013 1:39 pm

Regex help

Post by Minouch »

Hi;
I need to grab any the format ten numbers then a dot then jpg or png inside the {image}{/image} tag, for example :

1. {image}1390324469.jpg{/image} should return 1390324469.jpg without spaces
2. {image} 1390324469.jpg {/image} should return 1390324469.jpg without spaces
3. {image}unicode or non numbners characters1390324469.jpgunicode or non numbers characters{/image} should return 1390324469.jpg without spaces

This one "#{image}(.*?){\/image}#i", does only work in the first case and doesn't work in the second and third cases.
Thank you.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Regex help

Post by Celauran »

Code: Select all

php > $haystack = '{image}abcdefg1390324469.jpgabcdefg{/image}';
php > preg_match('/\{image\}.*?(\d{10}\.(jpg|png)).*?\{\/image\}/', $haystack, $matches);
php > print_r($matches);
Array
(
    [0] => {image}abcdefg1390324469.jpgabcdefg{/image}
    [1] => 1390324469.jpg
    [2] => jpg
)
php >
Minouch
Forum Newbie
Posts: 9
Joined: Thu Sep 12, 2013 1:39 pm

Re: Regex help

Post by Minouch »

Quick and wright answer, this one work,
"#{image}.*?(\d{10}\.(jpg|png)).*?{\/image}#i"

Thank you
Post Reply