Re: regular expression

From: Date: Thu, 01 Nov 2001 11:37:21 +0000
Subject: Re: regular expression
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-73142@lists.php.net to get a copy of this message
Hello, "Galkov Vladimir" <galkov@kraft-s.php.net> wrote in message news:20011101094130.22436.qmail@pb1.pair.com... > Need to remove all "../" "/.." from user inputing string to prevent him > walking and creating files&directories where I don't whant see them/him... > > The string: > > $path = > eregi_replace('([..]{2,})|([./]{2})|([../]{3,})|([/.]{2})|([/..]{3})', '', > $path); > > works good with any combinations ( ../../..qwert.txt => qwert.txt) untill > somth like "/../asd/../qwert.txt" will be entered ... (/../asd/../qwert.txt > => asdqwert.txt). > So the qwestion is how upgrade regular expression to remove all this > correctly (with all entered directory names but NOT assigned their names to > file name... > > Must do the operation: > /../asd/......../qwert.txt => qwert.txt > but not => /asd/qwert.txt or asdqwert.txt.ru > or > /................../asd/../qwert.txt.ru => qwert.txt.ru > but not => /asd/qwert.txt.ru or asdqwert.txt.ru > Try this: <? $string = "/../asd/......../qwert.txt"; $string = preg_replace("/(\.*\/\.?)+.*?(\.*\/)?/s", "", $string); echo $string; ?> May need some tweaking, let me know. ~James

« previous php.general (#73142) next »