preg_match

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
huvcbo
Forum Newbie
Posts: 5
Joined: Thu Apr 12, 2012 5:21 am

preg_match

Post by huvcbo »

Hello all!
I do like this,

$handle = opendir($dir);
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if ( strlen( $file ) && strstr( strtoupper($file), "ext") ) {
if( preg_match("/^TEXT.EXT/", $file) ) { //how
$fileArray[] = $file;
}
}
}
}
file names I'm after is the following
TEXT____1204131011599.EXT
TEXT123_1204131011599.EXT
TEXT12341204131011599.EXT
how do preg_match to look for it to work?

appreciate any help

huvcbo :D :D
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: preg_match

Post by Celauran »

Syntax blocks are your friend.

If you're trying to match any file that starts with TEXT and ends with .EXT, try this

Code: Select all

preg_match('/^TEXT.*\.EXT$/', $file);
huvcbo
Forum Newbie
Posts: 5
Joined: Thu Apr 12, 2012 5:21 am

Re: preg_match

Post by huvcbo »

Tanks for a quick reply
but I need to look for
TEXT____
TEXT123_
TEXT1234
also
1204131011599
then EXT

huvcbo
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: preg_match

Post by Celauran »

So then

Code: Select all

preg_match('/TEXT.*1204131011599\.EXT/', $file);
huvcbo
Forum Newbie
Posts: 5
Joined: Thu Apr 12, 2012 5:21 am

Re: preg_match

Post by huvcbo »

Thanks,
I must be more presic
first
TEXT____
TEXT123_
TEXT1234
then similar to this
[0-9]{13}
last
EXT.

sorry for my lack of clarity

huvcbo
preg_match('/TEXT.*1204131011599\.EXT/', $file);
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: preg_match

Post by Celauran »

So really /TEXT[0-9_]{4}[0-9]{13}\.EXT/
huvcbo
Forum Newbie
Posts: 5
Joined: Thu Apr 12, 2012 5:21 am

Re: preg_match

Post by huvcbo »

works almost
with
/TEXT[0-9_]{4}[0-9]{13}\.SELFSCAN/", $file)

I get
TEXT123_1204131011599.EXT
and
TEXT12341204131011599.EXT
but not
TEXT____1204131011599.EXT
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: preg_match

Post by Celauran »

Can't imagine why. All three work fine for me.
huvcbo
Forum Newbie
Posts: 5
Joined: Thu Apr 12, 2012 5:21 am

Re: preg_match

Post by huvcbo »

It works thanks
huvcbo
Post Reply