i am currently working on a script to match IT eqipment models from different suppliers, the idea is to remove the -XXX numbers at the end, the ending P or a P- in the middle of the name example models are
DH-HAC-HDBW3802EP-Z HAC-HDBW3802E-Z
DH-HAC-HDBW3802EP-ZH HAC-HDBW3802E-ZH
DH-HAC-HDW1000MP-028 HAC-HDW1000M
DH-HAC-HDW1000RP-028 HAC-HDW1000R
DH-HAC-HDW1100EMP-02 HAC-HDW1100EM
DH-HAC-HDW1100EMP-03 HAC-HDW1100EM
DH-HAC-HDW1100MP HAC-HDW1100M
DH-HAC-HDW1100MP-036 HAC-HDW1100M
DH-HAC-HDW1100RP-028 HAC-HDW1100R
DH-HAC-HDW1100RP-VF HAC-HDW1100R-VF
for now i am using a rather complicated code that i must admit, does work but i have a deep inside urge to regex it a little * i know, if it works, don't mess with it* The function to clean the endings of the names is looking like
function beautifyDahua($text)
{
$text = str_replace('DHI-', '', $text);
$text = str_replace('DH-', '', $text);
if (empty($text)) {
return 'n-a';
}
//if begins with IPC sau HAC, clean further
elseif (substr( $text, 0, 4 ) === "IPC-" OR substr( $text, 0, 4 ) === "HAC-") {
$text = str_replace('AP-028', 'A', $text);
$text = str_replace('AP-036', 'A', $text);
$text = str_replace('AP', 'A', $text);
$text = str_replace('BP-028', 'B', $text);
$text = str_replace('BP-036', 'B', $text);
$text = str_replace('BP', 'B', $text);
$text = str_replace('CP-', 'C-', $text);
$text = str_replace('DP-036', 'D', $text);
$text = str_replace('DP-', 'D-', $text);
$text = str_replace('EMP-03', 'EM', $text);
$text = str_replace('EMP-02', 'EM', $text);
$text = str_replace('EMP-', 'EM-', $text);
$text = str_replace('EP-036', 'E', $text);
$text = str_replace('EP-028', 'E', $text);
$text = str_replace('EP-03', 'E', $text);
$text = str_replace('EP-02', 'E', $text);
$text = str_replace('EP-', 'E-', $text);
$text = str_replace('EP', 'E', $text);
$text = str_replace('FP-03', 'F', $text);
$text = str_replace('FP-02', 'F', $text);
$text = str_replace('FP-', 'F-', $text);
$text = str_replace('FP', 'F', $text);
$text = str_replace('RMP-03', 'RM', $text);
$text = str_replace('RMP-02', 'RM', $text);
$text = str_replace('RMP-', 'RM', $text);
$text = str_replace('RMP', 'RM', $text);
$text = str_replace('RP-028', 'R', $text);
$text = str_replace('RP-036', 'R', $text);
$text = str_replace('RP-', 'R-', $text);
$text = str_replace('RP', 'R', $text);
$text = str_replace('SP-036', 'S', $text);
$text = str_replace('SP-028', 'S', $text);
$text = str_replace('SP-', 'S-', $text);
$text = str_replace('SP', 'S', $text);
$text = str_replace('SLP-03', 'SL', $text);
$text = str_replace('TP-', 'T-', $text);
$text = str_replace('MP-036', 'M', $text);
$text = str_replace('MP-028', 'M', $text);
$text = str_replace('MP', 'M', $text);
return $text;
}
else {
return $text;
}
}
For the numbers i have a regex like \b-0(\d|\d\d)\b
But for the P situation i am in over my head.
Any advice on how to tackle this?
-with something else (i.e. the first character matched?)