Skip to content
Advertisement

Remove blacklisted terms from string then eliminate unnecessary spaces

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"

Advertisement

Answer

  • 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.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement