0

我正在尝试从变量中删除标点符号但它不起作用,我已经尝试了我能想到的一切。我已经很累了,我确定这是我没有看到的东西。任何帮助将不胜感激。

代码:

$memberlinksafe = $_POST["entity_name"];

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

我没看到什么?它只是返回原始$_POST变量

4

1 回答 1

1

为什么

$memberlinksafe = str_replace(" +", "", $memberlinksafe);

并不是

// This function removes spaces, \t (tabs), \r and \n. All are spaces!
$memberlinksafe = preg_replace('~[\\s]+~', '', $memberlinksafe);

剥离空间?

更新

// Remove punctuation
$memberlinksafe = preg_replace('~[[:punct:]]+~', '', $memberlinksafe);
// Remove spaces
$memberlinksafe = preg_replace('~[\\s]+~', '', $memberlinksafe);
// Lowercase
$memberlinksafe = strtolower($memberlinksafe);

或者做:

// Keep only alphanumerics and lowercase it
$memberlinksafe = strtolower(preg_replace('~[^[:alnum:]]+~', '', $memberlinksafe));

为什么不\W

因为这:

// outputs 'testing_something'
var_dump(preg_replace('~[\\W]+~', '', 'testing_something'));
// outputs 'testingsomething'
var_dump(preg_replace('~[[:punct:]]+~', '', 'testing_something'));

_实际上是标点符号。

于 2013-06-17T19:20:24.617 回答