Reputation: 683
How to format specific parts of a string?
In this case, I have a form that asks users for an address, and it posts to php as a variable that I catch with $_POST
, and turn it into a variable which I output like the following to make it look cleaner:
$receive_address = ucwords(strtolower(str_replace('.','',$_POST['send_address'])));
The address would be entered into the form like this...
100 john st., toronto, on
...and be outputted as this...
100 John St, Toronto, On
If I enter 100 john st., toronto
, on, or 100 john st., toronto, ontario
, I want the final result to be this:
100 John St, Toronto, ON
Literally the only difference is that the final result will conclude with the province in 2 letters, capitalized.
I want to take it a step further and check to see if there is an "On" in there at the end, and if there isn't to add it, and if there is, to capitalize both letters.
Is this possible, and how would this work?
Here's my attempt at figuring this out:
function findProvCode($address_data) {
$prov_codes = array('NL','PE','NS','NB','QC','ON',' MB','SK','AB','BC','YT','NT','NU')
if (stripos($address_data, $prov_codes)) {
// still need to find out which of the prov codes it is...
// $specify_code = something to do with finding out which code this address already had
str_replace('$address_data',strtoupper($specify_code),$address_data);
}
}
Here's a PHP sandbox of where I'm at so far... someone had posted an answer with some ideas but deleted it (assuming because it didn't work the way they had intended), those ideas were useful though: http://sandbox.onlinephpfunctions.com/code/36f1e07e2e52573e230da4dda49320c564a818cd
The workflow seems simple...
1 - check for province or province code
2 - if province code, uppercase it
3 - if whole word, change to province code and uppercase province code
Just not sure how to do this.
I know this really isn't that important in reality... I just want to learn how to manipulate a string this way.
Upvotes: 3
Views: 164
Reputation: 691
Do this at the end of your code:
$address_string = "100 John St, Toronto, On";
$address_array = explode(',', $address_string);
array_push($address_array, strtoupper(array_pop($address_array)));
$address_string = implode(',', $address_array);
OR, if you prefer a function:
function CapProvCode($address_string){
$address_array = explode(',', $address_string);
array_push($address_array, strtoupper(array_pop($address_array)));
return implode(',', $address_array);
}
Where $address_string is the string which the two last letters will be capitalized.
I've tried them here and I got the following output after a echo $address_string;
100 John St, Toronto, ON
Explaning what happened:
The explode function breaks the string everytime it finds a ',' (delimiter) and return the pieces into an array. So, after the first line, $address_array is:
Array
(
[0] => 100 John St
[1] => Toronto
[2] => On
)
On second line we will analyse piece after piece. The most inside nest, array_pop, take out the last item of $address_array
and we got this
second line in progress
array_push($address_array, strtoupper(" On"));
print_r() of $address_array at this moment
Array
(
[0] => 100 John St
[1] => Toronto
)
Now the function strtoupper() capitalize the string " On". On the outter nest, array_push insert the string capitalized at the last position of $address_array
:
print_r() of $address_array after second line is evaluated
Array
(
[0] => 100 John St
[1] => Toronto
[2] => ON
)
Then, on third line, implode make the oposit of explode, join all pieces of an array into a string and separating them by ','. That's it!
Solving with Regular Expressions
To work with the possibility of a string like 100 John St, Toronto, On, Canada
, you have to deal with Regular Expressions. They are really not easy to understand and even harder to write, but definetly worth to learn how and when to use them. Regex are useful when you are dealing with string patterns.
Php has a native function to replace strings using regex: preg_replace(). Take a look at this code, it should do the trick:
$address_string[] = "100 John St, Toronto, On, Canada";
$address_string[] = "100 John St, Toronto, on, Canada";
$address_string[] = "100 John St, Toronto, On";
$address_string[] = "100 John St, Toronto, on";
foreach($address_string as $value){
echo preg_replace('/, [A-za-z]{2}(,|$)/e', 'strtoupper("$0")', $value);
echo "<br>";
}
I executed it and the output is:
100 John St, Toronto, ON, Canada
100 John St, Toronto, ON, Canada
100 John St, Toronto, ON
100 John St, Toronto, ON
I hope it works for all cases. Don't give up easily when trying to understand regex, they are really hard. This can help you.
Upvotes: 2
Reputation: 41820
Since you specifically asked about string manipulation, this is one way to do it using (mostly) string functions.
$codes = array('NL','PE','NS','NB','QC','ON','MB','SK','AB','BC','YT','NT','NU');
$province = strtoupper(substr($address, -2)); // take last 2 chars and convert to UC
if (in_array($province, $codes)) { // try to find it in the codes array
$address = substr($address, 0, -2) . $province; // replace last 2 chars with UC version
}
Not only string functions because I don't think there really is a better way than in_array
to verify that a string is part of a certain set of strings. You could do something like
if (strpos('NL|PE|NS|NB|QC|ON|MB|SK|AB|BC|YT|NT|NU', $province) !== false) { ...
but regardless of the delimiter used, matching an arbitrary string against a longer string instead of an array of strings can theoretically yield false positives.
Upvotes: 1
Reputation: 92894
You can use "relations map" array in such manner array('toronto' =>'ON', ...)
to define which province the certain town is related to:
function findProvCode($address_data) {
$prov_codes = array('toronto' =>'ON', 'NL','PE','NS','NB','QC','MB','SK','AB','BC','YT','NT','NU');
$words = explode(" ", ucwords($address_data));
$last_index = count($words) - 1;
if (in_array(strtoupper($words[$last_index]), $prov_codes)) {
$words[$last_index] = strtoupper($words[$last_index]);
} elseif (array_key_exists(strtolower($words[$last_index]), $prov_codes)) {
$words[] = $prov_codes[strtolower($words[$last_index])];
}
return implode(" ", $words);
}
echo findProvCode("100 john st., toronto, on") . "<br>";
echo findProvCode("100 john st., toronto");
The output:
100 John St., Toronto, ON
100 John St., Toronto ON
Upvotes: 1