Regular Expressions

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
Khairul Alam
Forum Newbie
Posts: 12
Joined: Tue Sep 09, 2003 6:27 am

Regular Expressions

Post 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
jigaruu
Forum Newbie
Posts: 21
Joined: Wed Feb 18, 2004 11:46 pm
Contact:

Post 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
User avatar
cybaf
Forum Commoner
Posts: 89
Joined: Tue Oct 01, 2002 5:28 am
Location: Gothenburg Sweden

Post 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
Khairul Alam
Forum Newbie
Posts: 12
Joined: Tue Sep 09, 2003 6:27 am

Post by Khairul Alam »

Thanks ALL


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


Have a good understanding.
Post Reply