Can't remove dashes (-) from string

2019-02-21 05:51发布

The following function strips some words into an array, adjusts whitespaces and does something else I need. I also need to remove dashes, as I write them as words too. But this function doesn't remove dashes. What's wrong?

function stripwords($string) 
{ 
  // build pattern once 
  static $pattern = null; 
  if ($pattern === null) { 
    // pull words to remove from somewhere 
    $words = array('alpha', 'beta', '-');  
    // escape special characters 
    foreach ($words as &$word) { 
      $word = preg_quote($word, '#'); 
    } 
    // combine to regex 
    $pattern = '#\b(' . join('|', $words) . ')\b\s*#iS'; 
  } 

  $print = preg_replace($pattern, '', $string);
  list($firstpart)=explode('+', $print);
  return $firstpart;

}

2条回答
三岁会撩人
2楼-- · 2019-02-21 06:20

To answer your question, the problem is the \b which designates a word boundary. If you have a space before or after the hyphen, it won't remove it as in " - ", the word boundary doesn't apply.

From http://www.regular-expressions.info/wordboundaries.html:

There are three different positions that qualify as word boundaries:

  1. Before the first character in the string, if the first character is a word character.
  2. After the last character in the string, if the last character is a word character.
  3. Between two characters in the string, where one is a word character and the other is not a word character.

A "word character" is a character that can be used to form words.

A simple solution:

By adding \s along with \b to your pattern and using a positive look-behind and a positive look-ahead, you should be able to solve your problem.

$pattern = '#(?<=\b|\s|\A)(' . join('|', $words) . ')(?=\b|\s|\Z)\s*#iS'; 
查看更多
疯言疯语
3楼-- · 2019-02-21 06:20

Nowhere in your regex pattern are you looking for dashes. Why not just do

$string = str_replace('-', '', $string);

after you do your regex stuff?

查看更多
登录 后发表回答