find two capital letters in a string.
Moderator: General Moderators
- SmokyBarnable
- Forum Contributor
- Posts: 105
- Joined: Wed Nov 01, 2006 5:44 pm
find two capital letters in a string.
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.
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.
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];
?>
- SmokyBarnable
- Forum Contributor
- Posts: 105
- Joined: Wed Nov 01, 2006 5:44 pm
Re: find two capital letters in a string.
Thank you!
Re: find two capital letters in a string.
Will that work if the entire string is in capitals?