find two capital letters in a string.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
SmokyBarnable
Forum Contributor
Posts: 105
Joined: Wed Nov 01, 2006 5:44 pm

find two capital letters in a string.

Post 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.
User avatar
Kadanis
Forum Contributor
Posts: 180
Joined: Tue Jun 20, 2006 8:55 am
Location: Dorset, UK
Contact:

Re: find two capital letters in a string.

Post 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];
?>
 
User avatar
SmokyBarnable
Forum Contributor
Posts: 105
Joined: Wed Nov 01, 2006 5:44 pm

Re: find two capital letters in a string.

Post by SmokyBarnable »

Thank you!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: find two capital letters in a string.

Post by onion2k »

Will that work if the entire string is in capitals?
Post Reply