another regexp question

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
mortz
Forum Newbie
Posts: 10
Joined: Wed May 19, 2004 4:23 am
Location: Norway

another regexp question

Post by mortz »

I've played some with regular expressions and found out that this line works:

Code: Select all

<?php
if (ereg("[h][t][t][p][:][/][/]", $page) || ereg("^[/]", $page) || ereg("[.][/]", $page) || ereg("[.][.][/]", $page)) echo "ERROR!";
?>
There must be an easier way, right?
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Re: another regexp question

Post by redmonkey »

mortz wrote:I've played some with regular expressions and found out that this line works:

Code: Select all

<?php
if (ereg("[h][t][t][p][:][/][/]", $page) || ereg("^[/]", $page) || ereg("[.][/]", $page) || ereg("[.][.][/]", $page)) echo "ERROR!";
?>
There must be an easier way, right?
Your code is equivalent to...

Code: Select all

<?php
if (preg_match('/(http:\/\/|^\/|\.+?\/)/', $page))
{
  echo 'ERROR!';
}
?>
mortz
Forum Newbie
Posts: 10
Joined: Wed May 19, 2004 4:23 am
Location: Norway

Post by mortz »

Thanks!

All those strange characters gives me a headache :P
Guess I'll figure out how they work soon! :)
Post Reply