开发者

What could an alternative be for ereg("(\.)([a-z0-9]{3,5})$", $file_name) with preg_match()?

As we know that PHP ereg() function is no more a part of PHP 5.3.x and I need an alternative for the following code with PHP 开发者_Python百科preg_match():

ereg("(\.)([a-z0-9]{3,5})$", $file_name)

Any help will greatly be appreciated.

Thanks


This should do it (if I understood the original regex properly)

preg_match("/\\.([a-z0-9]{3,5})$/", $file_name)

It matches a string which ends with a . followed by 3, 4 or 5 numbers or lower case letters, and those last letters/numbers will be in the first matching group now.

If you're looking to grab the extension of a file, perhaps this article might help: http://cowburn.info/2008/01/13/get-file-extension-comparison/

The TLDR version is this:

$ext = pathinfo($file_name, PATHINFO_EXTENSION);


preg_match("/(\.)([a-z0-9]{3,5})$/", $file_name);

//dot will be obtained with $file_name[1] and these characters with $file_name[2]//
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜