如何获得公司名单的名称?

时间:2019-01-08 21:32:54

标签: php regex string preg-replace str-replace

我想创建一个简化公司名称(例如Apple Inc.,Microsoft Corporation,Advanced Micro Devices,Inc.)的函数,并创建一个小写字符串以嵌入URL(例如Apple,Microsoft,Advanced-微型设备)。

function cleanCompany($companyName){
  $companyName= strtolower($companyName);
  $companyName=preg_replace('/(!||&||,||\.||\'||\"||\(||\))/i', '', $companyName);
  $companyName=str_replace(array('company',' corporation',', inc.',' inc.',' inc',' ltd.',' limited',' holding',' american depositary shares each representing one class a.',' american depositary shares each representing one class a',' american depositary shares each representing two',' american depositary shares each representing 2',' class a',' s.a.'), '', $companyName);
  $companyName=preg_replace('/(\s+)/i', '-', $companyName);
  return $companyName; 
}

公司名称在此链接中:https://iextrading.com/trading/eligible-symbols/

此功能仍然存在我要解决的问题:

$companyName=str_replace(array('---','--'), array('-'), $companyName);

如何改进此功能或完成此任务?

1 个答案:

答案 0 :(得分:0)

根据Barmar的建议,我修改了该功能,效果很好。

function slugCompany($c){
  $c= strtolower($c);
  $c=preg_replace('/[^\da-z\s]/i', '', $c);
  $c=str_replace(array('company',' corporation',', inc.',' inc.',' inc',' ltd.',' limited',' holding',' american depositary shares each representing one class a.',' american depositary shares each representing one class a',' american depositary shares each representing two',' american depositary shares each representing 2',' class a',' s.a.'), '', $c);
  $c=preg_replace('/(\s+)/i', '-', $c);
  return $c; 
}

此外,我添加了一个循环,将'--'替换为'-'

for ($i=0; $i < 5; $i++) { 
  if(strpos($c,'--')!==false){
    $c=str_replace('--','-', $c);
  }else{
    break;
  }
}

我尝试了另一种方式

function slugCompany($c){
  $c= strtolower($c);
  $c=preg_replace('/[^\da-z\s]/i', '', $c);
  $words='11000th|american|and|a|beneficial|bond|b|class|common|company|corporation|corp|commodity|cumulative|co|c|daily|dep|depositary|depository|debentures|diversified|due|d|each|etf|equal|equity|exchange|e|financial|fund|fixedtofloating|fixed|floating|f|group|g|healthcare|holdings|holding|h|inc|incorporated|interests|interest|in|index|income|i|junior|j|k|liability|limited|lp|llc|ltd|long|l|markets|maturity|municipal|muni|monthly|m|noncumulative|notes|no|n|of|one|or|o|portfolio|pay|partnership|partner|par|perpetual|per|perp|pfd|preference|preferred|p|q|redeemable|repstg|representing|represents|rate|r|sa|smallcap|series|shs|shares|share|short|stock|subordinated|ser|senior|s|the|three|term|to|traded|trust|two|t|ultrashort|ultra|u|value|v|warrant|weight|w|x|y|z';
  $c=preg_replace('/\b('.$words.')\b/i', '', $c);
  $c=preg_replace('/(\s+)/i', '-', trim($c));
  return $c; 
}
相关问题