Reputation: 263
I've values in database
Buyer
Client
Shipper
Consignee
Forwarding Agent
Notification Company
I want BU
from buyer SH
from shipper CN
from consignee FA
from forwarding agent and NC
from notification company with the help of PHP language so how could I achieve this? Right now I'm getting full name so I want two characters from different locations.
Upvotes: 0
Views: 47
Reputation: 3868
<?php $myLoc = "Consignee"; // put your character...
$words = explode(" ",$myLoc);
count($words);
if($myLoc == "Consignee"){
$acronym = $myLoc{0}.$myLoc{2};
}
else if( count($words) >1){
$acronym = "";
foreach ($words as $w) {
$acronym .= $w[0];
}
}else{
$acronym = substr($myLoc, 0,2);
}
echo $acronym;
?>
Upvotes: 1
Reputation: 494
You can use this method
function getAcronym($word){
$word = trim($word);
if(strpos($word," ") >0){
$acronym = strtoupper(substr($word,0,1).substr($word,strpos($word," ")+1,1));
} else{
$acronym = strtoupper(substr($word,0,2));
}
return $acronym;
}
print_r(getAcronym("Buyer")); // Gives BU
print_r(getAcronym("Notification Company")); // Gives NC
However for Consingment it will return CO an not CN as you mentioned. If you need specific characters for each word and not according to a single rule, you can either use a separate table where you can store Two characters short forms for each term and look up the table when required.
Upvotes: 0