PHP条带标点符号

tes*_*est 31 php regex

假设我有这个:

$hello = "Hello, is StackOverflow a helpful website!? Yes!";
Run Code Online (Sandbox Code Playgroud)

我想删除标点符号,因此输出为:

hello_is_stackoverflow_a_helpful_website_yes
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

Wh1*_*Ck5 52

# to keep letters & numbers
$s = preg_replace('/[^a-z0-9]+/i', '_', $s); # or...
$s = preg_replace('/[^a-z\d]+/i', '_', $s);

# to keep letters only
$s = preg_replace('/[^a-z]+/i', '_', $s); 

# to keep letters, numbers & underscore
$s = preg_replace('/[^\w]+/', '_', $s);

# same as third example; suggested by @tchrist; ^\w = \W
$s = preg_replace('/\W+/', '_', $s);
Run Code Online (Sandbox Code Playgroud)

对于字符串

$s = "Hello, is StackOverflow a helpful website!? Yes!";
Run Code Online (Sandbox Code Playgroud)

结果(对于所有例子)是

Hello_is_StackOverflow_a_helpful_website_Yes_

请享用!


Raf*_*ler 15

function strip_punctuation($string) {
    $string = strtolower($string);
    $string = preg_replace("/[:punct:]+/", "", $string);
    $string = str_replace(" +", "_", $string);
    return $string;
}
Run Code Online (Sandbox Code Playgroud)

首先将字符串转换为小写,然后删除标点符号,然后用下划线替换空格(这将处理一个或多个空格,因此如果有人放置两个空格,它将仅被一个下划线替换).

  • 如果你想*可读*,那么你将使用完整的属性名称:`\ p {General_Category = Punctuation}`,它通常缩写为二进制属性,如`\ p {Punctuation}`.不幸的是,PCRE没有良好的Unicode属性支持.我永远不会信任POSIX charclasses的原因是因为它们太容易因供应商区域设置和用户设置而破损.它几乎从不正确处理Unicode,即使这是[UTS#18的RL1.2](http://unicode.org/reports/tr18/#Compatibility_Properties)所要求的.我不信任和误解任何不做Unicode的事情. (2认同)
  • 出于某种原因, /[:punct:]+/ 抛出了语法错误,但是 /[[:punct:]]+/ 没有。 (2认同)

qua*_*tme 10

没有正则表达式:

<?php
  $hello = "Hello, is StackOverflow a helpful website!? Yes!"; // original string
  $unwantedChars = array(',', '!', '?'); // create array with unwanted chars
  $hello = str_replace($unwantedChars, '', $hello); // remove them
  $hello = strtolower($hello); // convert to lowercase
  $hello = str_replace(' ', '_', $hello); // replace spaces with underline
  echo $hello; // outputs: hello_is_stackoverflow_a_helpful_website_yes
?>
Run Code Online (Sandbox Code Playgroud)