PHP Regular Expression
I am trying to write a regular expression to validate file names on a file system.
Examples of valid file names are
- tapa_newcougar_org.png
- tapa_lamborghini-talk_com.png
- tapa_clubfrontier_org.png
The logic be开发者_如何学编程hind valid is the image starts with tapa followed by an underscore. Then the domain name followed by _ the tld (com, org, net)
Thanks
preg_match('/^tapa_[a-z0-9-_]+?_(com|org|net)\.png$/', $string);
That ought to do it (tested). If you want to match capital letters too, add the i
(case insensitive) flag like this:
preg_match('/^tapa_[a-z0-9-_]+?_(com|org|net)\.png$/i', $string);
For a graphical representation of how this works, you can paste it in here: http://strfriend.com
You could try:
preg_match('/^tapa_.+_(com|org|net)\.png$/', $filename);
^tapa_[a-zA-Z0-9\-_]+_(com|org|net)\.png$
should work for you.
精彩评论