Preg_Replace()带有符号的数组

时间:2018-11-12 22:30:13

标签: php if-statement foreach preg-replace

我正在使用foreach循环和if statements从冗长的XML文档中提取数据。成功提取数据后,我将对其进行格式化,主要是使用preg_replace,因为很多数据都存储为缩写,而我需要完整的单词或短语。我已经成功创建了一个preg_replace数组,其中包括我所有需要的替换。但是,我正在使用w/之类的缩写。我希望将w/替换为with,将w/OD替换为with overdrive。下面是我的代码和输出。

我的php:

<?php

$patterns = [
 '/\b(pwr|pwer)\b/i' => 'Power',
 '/CVT/' => 'Automatic',
 '/@/' => 'at',

];

echo "<i>All Tech Specs:</i>.<br><br>";

 foreach ($finaltitle as $titlenumber => $titlename){
   foreach ($techData as $tsnumber => $tsname){
     if ($tsnumber == $titlenumber){
       if(empty($tsname) or $tsname === '- TBD -' or $tsname ==='N/A') continue;
         $tsname =  preg_replace(array_keys($patterns), $patterns, $tsname);
           $finaltsdata = "<b>".$titlename." (".$tsnumber.") "."</b>: ".$tsname."<br>";

    echo $finaltsdata;
   }
  }
 }

?>

输出:

Vehicle Name (1) : Toyota Corolla
Body Style (2) : Sedan
Drivetrain (6) : Front Wheel Drive
EPA Classification (7) : Compact Cars
Passenger Capacity (8) : 5
Passenger Volume (9) : 97.5
Base Curb Weight (10) : 2840
Engine Type (41) : Regular Unleaded I-4
Displacement (42) : 1.8 L/110
Fuel System (43) : Sequential MPI
SAE Net Horsepower @ RPM (48) : 132 at 6000
SAE Net Torque @ RPM (49) : 128 at 4400
Trans Type (52) : 1
Trans Description Cont. (53) : Automatic w/OD

如果将'/w//' => 'with',添加到$patterns数组中,则会由于包含/而导致错误。关于如何解决此问题的任何想法将不胜感激!

0 个答案:

没有答案