PHP条带标点符号

时间:2011-04-16 22:03:29

标签: php regex

假设我有这个:

$hello = "Hello, is StackOverflow a helpful website!? Yes!";

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

hello_is_stackoverflow_a_helpful_website_yes

我该怎么做?

4 个答案:

答案 0 :(得分:51)

# 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);

表示字符串

$s = "Hello, is StackOverflow a helpful website!? Yes!";

结果(对于所有示例)是

  

Hello_is_StackOverflow_a_helpful_website_Yes _

享受!

答案 1 :(得分:15)

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

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

答案 2 :(得分: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
?>

答案 3 :(得分:3)

我会选择这样的事情:

$str = preg_replace('/[^\w\s]/', '', $str);

我不知道这比你想要的更广泛,但听起来就像你想要做的那样。

我还注意到你已经用样本中的下划线替换了空格。我用的代码是:

$str = preg_replace('/\s+/', '_', $str);

请注意,这也会将多个空格折叠为一个下划线。