shahzad429 Posted June 27, 2012 Share Posted June 27, 2012 I have a 5 letter string eg: etd00 and AA001 there is any way in PHP to check first 2 letters are capital or not? Thanks, Shahzad Link to comment https://forums.phpfreaks.com/topic/264857-how-to-check-first-2-letter-of-string-is-capital-or-not/ Share on other sites More sharing options...
xyph Posted June 27, 2012 Share Posted June 27, 2012 <?php $string1 = 'etd000'; $string2 = 'AA001'; function check( $string ) { $a = ord($string[0]); $b = ord($string[1]); return $a > 64 && $a < 91 && $b > 64 && $b < 91; } var_dump(check($string1)); var_dump(check($string2)); function check_alternate($string) { return (bool) preg_match('/^[A-Z]{2}/', $string); } var_dump(check_alternate($string1)); var_dump(check_alternate($string2)); ?> Link to comment https://forums.phpfreaks.com/topic/264857-how-to-check-first-2-letter-of-string-is-capital-or-not/#findComment-1357331 Share on other sites More sharing options...
vikrantmohite Posted June 27, 2012 Share Posted June 27, 2012 <?php $string ="Anything you want here"; //your string here $array=str_split($string); If(ord($array[0])>=65 AND ord($array[0])<=90) echo "First letter is Capital"; If(ord($array[1])>=65 AND ord($array[1])<=90) echo "Second letter is Capital"; ?> Link to comment https://forums.phpfreaks.com/topic/264857-how-to-check-first-2-letter-of-string-is-capital-or-not/#findComment-1357335 Share on other sites More sharing options...
requinix Posted June 27, 2012 Share Posted June 27, 2012 Rather than deal with ASCII numbers, how about a plain and simple $string = "etd00"; $condition = (/* strlen($string) >= 2 && */ $string[0] >= 'A' && $string[0] = 'A' && $string[1] Link to comment https://forums.phpfreaks.com/topic/264857-how-to-check-first-2-letter-of-string-is-capital-or-not/#findComment-1357338 Share on other sites More sharing options...
shahzad429 Posted June 27, 2012 Author Share Posted June 27, 2012 thank you very much guys. Link to comment https://forums.phpfreaks.com/topic/264857-how-to-check-first-2-letter-of-string-is-capital-or-not/#findComment-1357345 Share on other sites More sharing options...
Psycho Posted June 27, 2012 Share Posted June 27, 2012 And why not just use a comparison using strtoupper()? function checkFirstTwo($string) { $firstTwo = substr($string, 0, 2); return ($firstTwo == strtoupper($firstTwo)); } Link to comment https://forums.phpfreaks.com/topic/264857-how-to-check-first-2-letter-of-string-is-capital-or-not/#findComment-1357355 Share on other sites More sharing options...
Pikachu2000 Posted June 27, 2012 Share Posted June 27, 2012 And yet another way . . . if( ctype_upper(substr($string, 0, 2)) ) { Link to comment https://forums.phpfreaks.com/topic/264857-how-to-check-first-2-letter-of-string-is-capital-or-not/#findComment-1357384 Share on other sites More sharing options...
Psycho Posted June 27, 2012 Share Posted June 27, 2012 And yet another way . . . if( ctype_upper(substr($string, 0, 2)) ) { Totally forgot about ctype_upper() - good call. Link to comment https://forums.phpfreaks.com/topic/264857-how-to-check-first-2-letter-of-string-is-capital-or-not/#findComment-1357462 Share on other sites More sharing options...
shahzad429 Posted June 28, 2012 Author Share Posted June 28, 2012 And yet another way . . . if( ctype_upper(substr($string, 0, 2)) ) { thanks Pikachu2000 it is working like a charm thank you again every one for all your help and support. Link to comment https://forums.phpfreaks.com/topic/264857-how-to-check-first-2-letter-of-string-is-capital-or-not/#findComment-1357629 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.