使用 PHP 从不同位置获取两个字符


get two characters from different locations using php

我在数据库中有值

Buyer Client Shipper Consignee Forwarding Agent Notification Company

我想在PHP语言的帮助下从买方SH托运人CN收货人FA货运代理和通知公司NC BU,那么我该如何实现这一目标?现在我得到了全名,所以我想要来自不同位置的两个角色。

<?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;
 ?>

您可以使用此方法

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

但是,对于 Consingment,它将返回 CO 而不是您提到的 CN。如果每个单词都需要特定的字符,而不是根据单个规则,则可以使用单独的表,您可以在其中存储每个术语的两个字符短格式,并在需要时查找该表。