Dimwhit Posted October 16, 2008 Share Posted October 16, 2008 I'm not sure how to word this well enough to even perform a search, but here's what I want to do: I have a field whose value is a filename. I want to look at that name and return one set of code if the name starts with a letter (a-z) and another if it's a number (1-9). Something like: if ($filename starts with letter) { echo "code 1"; } else if ($filename starts with number) { echo "code 2"; } I just don't know how, or if, that's doable. Is there something boneheadedly simple I'm don't know about? Quote Link to comment https://forums.phpfreaks.com/topic/128713-solved-php-command-to-detect-numbers-vs-letters/ Share on other sites More sharing options...
discomatt Posted October 16, 2008 Share Posted October 16, 2008 I'd use this <pre><?php $str = '123abc'; if( ctype_digit( substr($str, 0, 1) ) === TRUE ) { echo 'Starts with a number'; } else { echo 'Starts with a non-number'; } ?></pre> Quote Link to comment https://forums.phpfreaks.com/topic/128713-solved-php-command-to-detect-numbers-vs-letters/#findComment-667079 Share on other sites More sharing options...
thebadbad Posted October 16, 2008 Share Posted October 16, 2008 You can access the first character with $filename[0], and then use ctype_alpha() and ctype_digit() to check for alphabetic or numeric chars (0-9). <?php $filename = 'alpha'; if (ctype_alpha($filename[0])) { //first char alphabetic } elseif (ctype_digit($filename[0])) { //first char numeric } ?> Quote Link to comment https://forums.phpfreaks.com/topic/128713-solved-php-command-to-detect-numbers-vs-letters/#findComment-667084 Share on other sites More sharing options...
Dimwhit Posted October 16, 2008 Author Share Posted October 16, 2008 OK, I'm not having luck with either these. Let me start with the first one. I'm assuming that in the following: $str = "123abc"; I need to plug in my variable for '123abc', so it would look something like: $str = "$filename"; Right? That should get the actual filename text (or numbers) for the script to work with. I keep getting the 'unexpected T_Variable error on that line (in both examples) every time. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/128713-solved-php-command-to-detect-numbers-vs-letters/#findComment-667124 Share on other sites More sharing options...
Dimwhit Posted October 16, 2008 Author Share Posted October 16, 2008 I'm obviously missing something (and given that I'm still learning my way through php, that's not surpnising). But this is the entirety of the php code I'm working with: <?php $searchcriteria = array(); $searchcriteriausingquotes = array(('file')); if (array_key_exists('file', $_GET)) { $file = $_GET['file']; if (strlen($file)) $searchcriteria['file'] = $file; } $str = $file; if (ctype_digit($str[0]) === TRUE ) { echo 'Starts with a number'; } else { echo 'Starts with a non-number'; } ?> I tried a hybrid of the two suggestions above, but I'm getting an 'unexpected T_String' error on the following line: if (ctype_digit($str[0]) === TRUE ) { Any thoughts on why? Quote Link to comment https://forums.phpfreaks.com/topic/128713-solved-php-command-to-detect-numbers-vs-letters/#findComment-667236 Share on other sites More sharing options...
Dimwhit Posted October 16, 2008 Author Share Posted October 16, 2008 OK, Got it to work. I had to use thebadbad's suggested, because the other one wasn't working. But I had some bad invisible characters that were causing the problem. Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/128713-solved-php-command-to-detect-numbers-vs-letters/#findComment-667293 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.