Page 1 of 1

Regular Expressions

Posted: Thu Feb 19, 2004 6:31 am
by Khairul Alam
Hello Viewer

I am a newcomer in PHP. I'v studied a lot about regular expressions from PHP manuals. But still now I am confused about this.

Would u pls anyaone describe some expressions by their own way how they easily realise this. So it'll be very helpful to understand for me.

Here some expressions I'v qouted below -

Code: Select all

<?php

if(preg_match('/^[0-9]{2}\/[0-9]{2}\/[0-9]{2}\s/', $line)){ 
    $validlines[] = $line; 
  } 

?>

Code: Select all

<?php

preg_replace('/\s{2,}/', ' ', $valid)

?>

Most of all, If anyone of u got any easier way to understand the Regular Expressions, pls mention it here.

Pls never mind of my poor english.

Thanks ALL

Posted: Thu Feb 19, 2004 7:08 am
by jigaruu
<?php
if(preg_match('/^[0-9]{2}\/[0-9]{2}\/[0-9]{2}\s/', $line))
{ $validlines[] = $line; }
?>

preg_match p regular expression match to each other
[0-9]{2} says 0 to 9 any and it should be two digits from 0 to 9 i.e. 19, 09 or 90 it can be any 2 digit no.
\ shash says allow / to divide
ans so on so
02/03/04
so any 2 no divide by any other 2 and then other 2

Posted: Thu Feb 19, 2004 10:07 am
by cybaf
in the first example (just to add to what jigaruu said) the ^ means that the match should occur in the beginning of the string.

The second example matches any 2 or more whitespaces. ie 2 tabs or spaces or new line characters...

check out

http://se.php.net/manual/sv/pcre.pattern.syntax.php

//cybaf

Posted: Fri Feb 20, 2004 1:25 am
by Khairul Alam
Thanks ALL


Now I am clear about the expressions what do they means.


Have a good understanding.