php php如何去掉标点符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5233734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:42:26  来源:igfitidea点击:

how to strip punctuation in php

phpstringstrip

提问by Okan Kocyigit

How can I strip punctuation except for these characters .=$'-%

除了这些字符,我怎样才能去掉标点符号 .=$'-%

回答by Waiyl Karim

Here's a neat way to do it:

这是一个巧妙的方法:

preg_replace("#[[:punct:]]#", "", $target);

回答by mario

Since you need to match some Unicode characters () it would be sensible to use a regular expression. The pattern \p{P}matches any known punctuation, and the assertion excludes your desired special characters from vanishing:

由于您需要匹配一些 Unicode 字符 ( ),因此使用正则表达式是明智的。该模式\p{P}匹配任何已知的标点符号,并且断言将您想要的特殊字符排除在消失之外:

 $text = preg_replace("/(?![.=$'%-])\p{P}/u", "", $text);

回答by Shikiryu

<?
$whatToStrip = array("?","!",",",";"); // Add what you want to strip in this array
$test = "Hi! Am I here?";
echo $test."\n\n";
echo str_replace($whatToStrip, "", $test);

Demo here

演示在这里

or, of course, shorter :

或者,当然,更短:

$test = str_replace(array("?","!",",",";"), "", $test);


Source from 1st example of str_replacemanual

来自str_replace手册的第一个示例

回答by AlexanderMP

preg_replace("[^-\w\d\s\.=$'%]",'',$subject)

although it would be more correct and easy to specify the characters you want to strip, instead of characters (from an unknown set) you don't want to strip.

尽管指定要删除的字符会更正确且更容易,而不是您不想删除的字符(来自未知集合)。

回答by AlexanderMP

Try:

尝试:

preg_replace("/[^\w-\p{L}\p{N}\p{Pd}$\.%']/", "", 'YOUR DATA');

You didn't mentioned if you wanted spaces or not, so that will strip that too.

你没有提到你是否想要空格,所以这也会去掉。

回答by Dieter Gribnitz

The problem:

问题:

Need to save string as alphaNum with specific punctuation and don't want to completely discard characters with special punctuation.

需要将字符串保存为带有特定标点符号的 alphaNum 并且不想完全丢弃带有特殊标点符号的字符。

The solution:

解决方案:

class ClassName {

  protected static $cleanChars = array(
    '&lt;' => '', '&gt;' => '', '&#039;' => '', '&amp;' => '',
    '&quot;' => '', 'à' => 'A', 'á' => 'A', '?' => 'A', '?' => 'A', '?' => 'Ae',
    '&Auml;' => 'A', '?' => 'A', 'ā' => 'A', '?' => 'A', '?' => 'A', '?' => 'Ae',
    '?' => 'C', '?' => 'C', '?' => 'C', '?' => 'C', '?' => 'C', '?' => 'D', '?' => 'D',
    'D' => 'D', 'è' => 'E', 'é' => 'E', 'ê' => 'E', '?' => 'E', 'ē' => 'E',
    '?' => 'E', 'ě' => 'E', '?' => 'E', '?' => 'E', '?' => 'G', '?' => 'G',
    '?' => 'G', '?' => 'G', '?' => 'H', '?' => 'H', 'ì' => 'I', 'í' => 'I',
    '?' => 'I', '?' => 'I', 'ī' => 'I', '?' => 'I', '?' => 'I', '?' => 'I',
    '?' => 'I', '?' => 'IJ', '?' => 'J', '?' => 'K','?' => 'K', '?' => 'K',
    '?' => 'K', '?' => 'K', '?' => 'K', '?' => 'N', '?' => 'N', '?' => 'N',
    '?' => 'N', '?' => 'N', 'ò' => 'O', 'ó' => 'O', '?' => 'O', '?' => 'O',
    '?' => 'Oe', '&Ouml;' => 'Oe', '?' => 'O', 'ō' => 'O', '?' => 'O', '?' => 'O',
    '?' => 'OE', '?' => 'R', '?' => 'R', '?' => 'R', '?' => 'S', '?' => 'S',
    '?' => 'S', '?' => 'S', '?' => 'S', '?' => 'T', '?' => 'T', '?' => 'T',
    '?' => 'T', 'ù' => 'U', 'ú' => 'U', '?' => 'U', 'ü' => 'Ue', 'ū' => 'U',
    '&Uuml;' => 'Ue', '?' => 'U', '?' => 'U', '?' => 'U', '?' => 'U', '?' => 'U',
    '?' => 'W', 'Y' => 'Y', '?' => 'Y', '?' => 'Y', '?' => 'Z', '?' => 'Z',
    '?' => 'Z', 'T' => 'T', 'à' => 'a', 'á' => 'a', 'a' => 'a', '?' => 'a',
    '?' => 'ae', '&auml;' => 'ae', '?' => 'a', 'ā' => 'a', '?' => 'a', '?' => 'a',
    '?' => 'ae', '?' => 'c', '?' => 'c', '?' => 'c', '?' => 'c', '?' => 'c',
    '?' => 'd', '?' => 'd', 'e' => 'd', 'è' => 'e', 'é' => 'e', 'ê' => 'e',
    '?' => 'e', 'ē' => 'e', '?' => 'e', 'ě' => 'e', '?' => 'e', '?' => 'e',
    '?' => 'f', '?' => 'g', '?' => 'g', '?' => 'g', '?' => 'g', '?' => 'h',
    '?' => 'h', 'ì' => 'i', 'í' => 'i', '?' => 'i', '?' => 'i', 'ī' => 'i',
    '?' => 'i', '?' => 'i', '?' => 'i', '?' => 'i', '?' => 'ij', '?' => 'j',
    '?' => 'k', '?' => 'k', '?' => 'l', '?' => 'l', '?' => 'l', '?' => 'l',
    '?' => 'l', '?' => 'n', 'ń' => 'n', 'ň' => 'n', '?' => 'n', '?' => 'n',
    '?' => 'n', 'ò' => 'o', 'ó' => 'o', '?' => 'o', '?' => 'o', '?' => 'oe',
    '&ouml;' => 'oe', '?' => 'o', 'ō' => 'o', '?' => 'o', '?' => 'o', '?' => 'oe',
    '?' => 'r', '?' => 'r', '?' => 'r', '?' => 's', 'ù' => 'u', 'ú' => 'u',
    '?' => 'u', 'ü' => 'ue', 'ū' => 'u', '&uuml;' => 'ue', '?' => 'u', '?' => 'u',
    '?' => 'u', '?' => 'u', '?' => 'u', '?' => 'w', 'y' => 'y', '?' => 'y',
    '?' => 'y', '?' => 'z', '?' => 'z', '?' => 'z', 't' => 't', '?' => 'ss',
    '?' => 'ss', 'ый' => 'iy', 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G',
    'Д' => 'D', 'Е' => 'E', 'Ё' => 'YO', 'Ж' => 'ZH', 'З' => 'Z', 'И' => 'I',
    'Й' => 'Y', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N', 'О' => 'O',
    'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T', 'У' => 'U', 'Ф' => 'F',
    'Х' => 'H', 'Ц' => 'C', 'Ч' => 'CH', 'Ш' => 'SH', 'Щ' => 'SCH', 'Ъ' => '',
    'Ы' => 'Y', 'Ь' => '', 'Э' => 'E', 'Ю' => 'YU', 'Я' => 'YA', 'а' => 'a',
    'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'yo',
    'ж' => 'zh', 'з' => 'z', 'и' => 'i', 'й' => 'y', 'к' => 'k', 'л' => 'l',
    'м' => 'm', 'н' => 'n', 'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's',
    'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c', 'ч' => 'ch',
    'ш' => 'sh', 'щ' => 'sch', 'ъ' => '', 'ы' => 'y', 'ь' => '', 'э' => 'e',
    'ю' => 'yu', 'я' => 'ya'
  );

  public static function clean($string, $allowed=array(), $base="a-zA-Z0-9 "){
    if(empty($allowed) && !$base){ return false; }
    $ignore = "";
    if(is_array($allowed)){
      foreach($allowed as $a){
        $ignore .= preg_quote($a);
      }
    }
    return preg_replace( "/[^{$base}{$ignore}\s]/", "", $string );
  }

  public static function alphaNum($string, $allowed=array(), $convert=false){
    if($convert){
      $string = strtr($string, self::$cleanChars);
    }
    return self::clean($string, $allowed, 'a-zA-Z0-9 ');
  }

}

Examples:

例子:

Strip all punctuation:

去掉所有标点符号:

ClassName::alpaNum($string);

ClassName::alpaNum($string);

Strip all punctuation but convert special chars:

去除所有标点符号但转换特殊字符:

ClassName::alphaNum($string, null, true);

ClassName::alphaNum($string, null, true);

Alpha Num + additional punctuation:

字母数字 + 附加标点符号:

ClassName::alphaNum($string, array('_', '-', ',', '.'));

ClassName::alphaNum($string, array('_', '-', ',', '.'));

Alpha Num + additional punctuation and convert:

Alpha Num + 附加标点符号并转换:

ClassName::alphaNum($string, array('_', '-', ',', '.'), true);

ClassName::alphaNum($string, array('_', '-', ',', '.'), true);

Conclusion: If you are expecting special chars and don't completely want to discard them you can convert them before checking alphaNum. (eg. on sanitizing file names etc.)

结论:如果您期待特殊字符并且不想完全丢弃它们,您可以在检查 alphaNum 之前转换它们。(例如,清理文件名等)

If discarding the special chars does not have any real impact and is not really expected on the system you can call it without conversion of punctuation to save processing power. (eg. on setting keys for large arrays from strings)

如果丢弃特殊字符没有任何实际影响并且不是真正期望在系统上使用,则可以在不转换标点符号的情况下调用它以节省处理能力。(例如,从字符串为大型数组设置键)

I got the cleanChars var from here: (I slightly modified it) https://github.com/vanillaforums/Garden/blob/master/library/core/class.format.php

我从这里得到了 cleanChars 变量:(我稍微修改了它) https://github.com/vanillaforums/Garden/blob/master/library/core/class.format.php