Ofir Attia
Ofir Attia

Reputation: 1275

Delete specific word from string by wordlist


I have this function to remove a specific word from a String, this function use a word list. so i have a query and i want to remove every type of string like (number,number)
this is mycode:

$wordlist = array("LIMIT", "0, 5");

 foreach ($wordlist as &$word) {
      $word = '/\b' . preg_quote($word, '/') . '\b/';
  }

 $sql = preg_replace($wordlist, '', $sql);
 var_dump($sql);

as you can see, if the function get LIMIT or 0,5 it will be removed, how i can remove any type of word like "number, number" so the query will be LIMIT 10, 40 it will be removed too.
to know if a string is with format of "is_number,is_number".
thanks.

Upvotes: 0

Views: 348

Answers (2)

Raza Ahmed
Raza Ahmed

Reputation: 2751

As described at http://snipplr.com/view/65038/ an easy way would be :

function stripCp($string )
{
  $wordlist = array("CP", "case", "postale","Case","Postale","cp");
  foreach($wordlist as $word)
  {
    $string = str_replace($word, "", $string);
  }
  return $string;
}

Upvotes: 2

pedak
pedak

Reputation: 91

Use preg_replace() to remove in regular expression marked string

$pattern = '/\d+,\s*\d+/';
$replacement = '';

//wordlist version
$wordlist = array("LIMIT", "0, 5");
foreach($wordlist as &$string){
    $string=preg_replace($pattern, $replacement, $string);
}
//query version
$query="SELECT * FROM table LIMIT 0, 5";
$result=preg_replace($pattern, $replacement, $query);

Upvotes: 0

Related Questions