I am trying to manipulate a multiple strings in PHP. I am extracting array data using foreach
loops and if statements
. Some of the array data is stored as abbreviations. I want to convert these abbreviations to the full word. One example is the abbreviation pwr
which stands for power
. The abbreviation can be found in a variety of different contexts. Below I will attach my PHP code followed by its respective output where you can see the variety of strings the pwr
abbreviation can be found in.
PHP:
<?php
foreach ($finaltitle as $titlenumber => $titlename){
foreach ($techData as $tsnumber => $tsname){
if ($tsnumber == $titlenumber){
if(empty($tsname) or $tsname === '- TBD -') continue;
$finaltsdata = "<b>".$titlename." (".$tsnumber.") "."</b>: ".strtolower($tsname)."<br>";
echo $finaltsdata;
}
}
}
?>
Output:
Suspension Type - Front (105) : 4-link,
Suspension Type - Rear (106) : trapezoidal link,
Suspension Type - Front (Cont.) (107) : pwr-adjustable sport
Suspension Type - Rear (Cont.) (108) : pwr-adjustable sport
Front Tire Size (140) : p235/40yr18
Rear Tire Size (141) : p235/40yr18
Spare Tire Size (142) : p235/40yr18
Front Wheel Size (156) : 18 x 8.0
Rear Wheel Size (157) : 18 x 8.0
Spare Wheel Size (158) : 18 x 8.0
Front Wheel Material (165) : alloy
Rear Wheel Material (166) : alloy
Spare Wheel Material (167) : alloy
Steering Type (176) : pwr rack & pinion
Steering Ratio (:1), Overall (178) : 16.3
Turning Diameter - Curb to Curb (184) : 37.7
Brake Type (186) : pwr
Notice how the abbreviation pwr
can be found in a variety of different contexts: alone, before a set of words, before a dash, etc. How can I create a statement within my foreach
code to convert all mentions of pwr
to power
?
Thanks!
$array = [
'/\b(pwr|Pwr|powr)\b/i' => 'power',
];
$tsname = preg_replace(array_keys($array), $array);
foreach ($finaltitle as $titlenumber => $titlename){
foreach ($techData as $tsnumber => $tsname){
if ($tsnumber == $titlenumber){
if(empty($tsname) or $tsname === '- TBD -') continue;
$finaltsdata = "<b>".$titlename." (".$tsnumber.") "."</b>: ".$tsname."<br>";
echo $finaltsdata;
}
}
}
As I said in the comments
$array = [
'/\bpwr\b/i' => 'power',
'/\bbrk\b/i' => 'brake'
];
$str = preg_replace(array_keys($array), $array, $str);
Then you just add more words and their replacement. Preg Replace is better because you can use \b
or the word boundary which prevents things like matching pwr
in pwrstr
or what have you. The \b
matches \W
or not \w
or in english (lol) anything not a-z
A-Z
0-9
and _
.
String replace could match a part of a word, which you don't really want to take the chance on.
One other advantage of preg_replace is this \b(pwr|powr)\b
where you could match both of these and replace them with the same thing. The ()
is a capture group, here I use it just to group the words so the \b
doesn't have to be repeated. And the |
is OR.
The last thing is you can use the i
flag (after the ending delimiter /foo/i
) to make it case insensitive. With str_ireplace()
, you can do the same, but you can't differentiate on a per match basis like you can with preg_replace()
.
Both work with arrays so you don't have to loop over the multiple replacement yourself with either str_repace()
or preg_replace()
.
If you need help, or just to test the Regular Expressions, this site is one of the best test Regex site around https://regex101.com/ (IMO). That said, there are some slight difference (probably nothing you'll notice), but another good site is this one http://preg_replace.onlinephpfunctions.com/ . If you look at the navigation on it, it has a bunch of PHP sandboxy stuff.
Cheers!