仅在php中删除特殊字符和数字

时间:2019-05-14 13:46:10

标签: php preg-replace

如何删除数字和特殊字符而不删除字符串中的空格?

例如:

 self.layer.cornerRadius = 12
 self.clipsToBounds = true
 for subview in subviews {
    if let imageView = subview as? UIImageView {
       imageView.layer.cornerRadius = 12
       imageView.clipsToBounds = true
    }
 }

我已经尝试了上面的代码,但是没有用。我也尝试阅读php手册,但我不太了解其中的内容。我在网上找到的所有示例都从字符串中删除了空格。谁能告诉我该怎么做或为我建议一些好书

非常感谢您。

1 个答案:

答案 0 :(得分:1)

您可以使用

$input = "Random string with random 98 and %$% output"; 
$filtered_input = trim(preg_replace("/\s*(?:[\d_]|[^\w\s])+/", "", $input));
echo $filtered_input;

输出:

Random string with random and output

请参见regex demoPHP demo

详细信息:

  • \s*-超过0个空格(在需要删除值之前)
  • (?:[\d_]|[^\w\s])+-一个或多个出现的数字或下划线或除单词和空格之外的任何字符。

trim函数删除所有导致的前导空格(如果有)。

相关问题