Page 1 of 1
find two capital letters in a string.
Posted: Mon Apr 07, 2008 4:32 am
by SmokyBarnable
I need to find the strpos of the first occurrence of two capital letters in a string. Is this possible? I'm trying to find the abbreviation of states in shipping addresses.
Re: find two capital letters in a string.
Posted: Mon Apr 07, 2008 5:29 am
by Kadanis
You could try using RegEx.
I had a toy around with this and using the belowI managed to pull out the first occurrence of 2 capitalized letters in any string.
Code: Select all
<?php
#pattern should only pick up on capital letters in pairs
$pattern = '@[A-Z]{2}@';
#address string
$address = "123 Some Street, Some City, FL, 45678";
#check string for match
preg_match($pattern, $address, $matches);
#will always be [0] unless you use preg_match_all
echo $matches[0];
?>
Re: find two capital letters in a string.
Posted: Mon Apr 07, 2008 6:13 am
by SmokyBarnable
Thank you!
Re: find two capital letters in a string.
Posted: Mon Apr 07, 2008 9:49 am
by onion2k
Will that work if the entire string is in capitals?