Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"

like image 264
Muamar Humaidi Avatar asked Jul 21 '20 12:07

Muamar Humaidi


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

like image 50
V-K Avatar answered Sep 29 '22 10:09

V-K


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

like image 22
nice_dev Avatar answered Sep 29 '22 10:09

nice_dev