【问题标题】:How to get the names of a list of companies?如何获取公司列表的名称?
【发布时间】:2019-06-03 15:09:51
【问题描述】:

我想创建一个函数来简化公司名称(例如 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);

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

【问题讨论】:

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


【解决方案1】:

根据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; 
}

【讨论】:

    猜你喜欢
    • 2017-02-24
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 2016-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多