【问题标题】:remove numbers and special characters from a string using php and regex [duplicate]使用php和正则表达式从字符串中删除数字和特殊字符[重复]
【发布时间】:2017-01-03 09:57:57
【问题描述】:

我在删除数字和特殊字符时遇到了一些问题。我想从输入中删除所有数字和特殊字符。这是我的代码:

$input = $_POST["input"];

function preprocessing($input){
    $input = trim(strtolower($input));
    $remove = '/[^a-zA-Z0-9]/s';
    $result = preg_split($remove, $input, -1, PREG_SPLIT_NO_EMPTY);
    for($i = 0; $i < count($resultl); $i++){
        $result[$i] = trim($result[$i]);
    }
    return $result;
}

字符串示例: qwd qwd qwdqd123 13#$%^&*) ADDA ''''

输出: 数组([0] => qwd [1] => qwd [2] => qwdqd123 [3] => 13 [4] => adda)

数字仍然出现在我的字符串上。如何解决这个问题? 之前谢谢你。

【问题讨论】:

  • 您可能想从您的模式中删除0-9。即/[^a-zA-Z]/,如果它真的只是你需要的大写和小写字母
  • 它有效。非常感谢@kennasoft。

标签: php regex


【解决方案1】:

检查一下

function clean($string) {
   $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.

   return preg_replace('/[^A-Za-z\-]/', '', $string); // Removes special chars.
}

【讨论】:

猜你喜欢
  • 2017-12-11
  • 1970-01-01
  • 2011-03-19
  • 2017-09-20
  • 2016-05-26
  • 2014-03-16
  • 2017-06-22
  • 1970-01-01
相关资源
最近更新 更多