olli460 Posted October 11, 2009 Share Posted October 11, 2009 Hello, Im trying to get a letter from a filename, The file name are structed as follows: I want to extract the (U) $name = "Random_File_Name_(U)_(Random Text).rar"; preg_match('([a-zA-Z])', $name, $flag); print_r($flag); But it dosen't work and im not really sure about how to do this kind of thing. Thanks in advance Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted October 11, 2009 Share Posted October 11, 2009 preg_match('%\(([a-zA-Z])\)%', $name, $flag); Try that Quote Link to comment Share on other sites More sharing options...
.josh Posted October 11, 2009 Share Posted October 11, 2009 is that "U" really surrounded by parenthesis? is "Random Text" really surrounded by parenthesis? Quote Link to comment Share on other sites More sharing options...
olli460 Posted October 11, 2009 Author Share Posted October 11, 2009 Thanks JAY6390 That works Great! I also have NL in some of the brackets, That code u gave me only seems to grab one of the letters, How can i match 2 letters? Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted October 11, 2009 Share Posted October 11, 2009 preg_match('%\(([a-zA-Z]+)\)%', $name, $flag); Quote Link to comment Share on other sites More sharing options...
cags Posted October 11, 2009 Share Posted October 11, 2009 The method suggested by JAY6390 will match 1 or more characters. If you need to make sure it's only 1 or two, one option (among several) would be to use... '%\(([a-zA-Z][a-zA-Z]?)\)%' Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted October 11, 2009 Share Posted October 11, 2009 or this...where 1 is the minimum and 2 is the maximum preg_match('%\(([a-zA-Z]{1,2})\)%', $name, $flag); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.