Remove blacklisted terms from string then eliminate unnecessary spaces

Viewed 331

I have an array of blacklisted terms:

$arrayBlacklist = array("Kota","Kab.","Kota Administrasi","KAB", "KOTA", "Kabupaten");

and I have a string to sanitize:

$city = "Kota Jakarta Selatan";
// also: "Kab. Jakarta Selatan", "Kota Administrasi Jakarta Selatan", ...

I just want to remove the $arrayBlacklist value if it's in the $city variable.

So, I get $city = "Jakarta Selatan"

4 Answers
$arrayBlacklist = array("Kota Administrasi", "Kota","Kab.","KAB", "KOTA", "Kabupaten");
rsort($arrayBlacklist);
$city = "Kota Jakarta Selatan";
        
$city = trim(preg_replace('/\s+/', ' ',str_replace($arrayBlacklist, '', $city)));

You can use https://www.php.net/manual/en/function.str-replace.php

str_replace can use arrays as search and replace statements.

  • Sort the array based on string length to avoid overlapping issues using usort.
  • preg_replace each of the string in a case-insensitive manner.
  • Finally, remove all double spaces with a single space using str_replace.

Snippet:

<?php

$arrayBlacklist = array("Kota","Kab.","Kota Administrasi","KAB", "KOTA", "Kabupaten","Jakarta");

usort($arrayBlacklist,function($a,$b){
    return strlen($b) <=> strlen($a);
});


$city = "Kota Jakarta Selatan kota Administrasi ki";
$city = " ". $city. " "; // add spaces to ease the matching

foreach($arrayBlacklist as $val){
   $city = preg_replace('/\s'.$val.'\s/i','  ',$city); // replace with double spaces to avoid recursive matching
}

$city = str_replace("  "," ",trim($city));
echo $city;

Update:

The preg_replace matches the strings as a string covered by space on both left and right hand sides since you sometimes have non-word characters too in your blacklisted strings. To ease the matching, we deliberately add leading and trailing spaces before the start of the loop.

Note: We replace the matched string in preg_replace with double spaces to avoid recursive matching with other strings.

Less elegant than other answer, but gets the job done.

$arrayBlacklist = ['Kota', 'Kab.', 'Kota Administrasi', 'KAB', 'KOTA', 'Kabupaten'];
$city = 'Kota Jakarta Selatan'; 

// make an array of words from the city name
$cityAsArray = explode(' ', $city);

foreach ($cityAsArray as $key => $part) {
    // check if word is in blacklist
    if (in_array($part, $arrayBlacklist)) {
        // remove from the array if it is blacklisted
        unset($cityAsArray[$key]);
    }
}

// convert the city name back to string
$city = implode(' ', $cityAsArray);

UPDATE: we can sort the blacklist array based on number of words, then string replacing each blacklisted string.

$arrayBlacklist = ["Kota", "Kab.", "Kota Administrasi", "KAB", "KOTA", "Kabupaten"];
$city = 'Kota Administrasi Jakarta Selatan';
usort($arrayBlacklist, function ($a, $b) {
    return substr_count($a, ' ') < substr_count($b, ' ');
});

foreach ($arrayBlacklist as $blacklist) {
    $city = trim(str_replace($blacklist, '', $city));
}

I think strtr() is the best tool for this job because:

  1. You don't need to pre-sort your blacklist array
  2. Longer matches will supersede shorter matches when replacing.

So effectively, you "translate", then trim leading/trailing spaces, then remove any internal redundant spaces.

Code: (Demo)

$arrayBlacklist = ["Kota Administrasi", "Kota","Kab.","KAB", "KOTA", "Kabupaten"];
$trans = array_fill_keys($arrayBlacklist, '');

$cities = [
    "Kota Jakarta Selatan",
    "Kota Administrasi Selatan",
    "Kab. What Kota Kab.",
    "KOTA Kota Coca Cola",
];
        
foreach ($cities as $city) {
    var_export(
        preg_replace('/\s{2,}/', ' ', trim(strtr($city, $trans)))
    );
    echo "\n";
}

Output:

'Jakarta Selatan'
'Selatan'
'What'
'Coca Cola'
Related