I believe this is what you are looking for:
ereg('^([[:alnum:]_/\-])*/?([[:alnum:]_\-]+\.php)?$', $str); Both this regular expression and the one you provided will only allow a period before the filename. Your regular expression will match a path with a filename but no extension, such as "whatever/whatever". It will also reject paths that begin with a /. Other than that, it appears okay, although verbose. Also, why not allow file/directory names to begin with a -, as this can occur in URIs?