如何删除所有垃圾字符,但保持货币符号在php正则表达式


How to remove all junk characters but keep currency symbols by regular expression in php?

下面是我的php代码

<?php
$output = "Clean this copy of invalid non ASCII ¥ äócharacters.";
$output = preg_replace('/[^('x20-'x7F'p{Sc})]/','',$output);    
echo($output);
?>

这里我想保持任何货币符号不变,需要删除垃圾字符。

对于这个问题,我应该在正则表达式中做哪些更改?

Thanks in advance

您需要将u修饰符添加到RegEx中,并且它工作得很好:

$output = 'Clean this copy of invalid non ASCII ¥$€ äócharacters';
$output = preg_replace('/[^('x20-'x7F'p{Sc})]/u','',$output);    

输出
Clean this copy of invalid non ASCII ¥$€ characters.

第一个问题,你想达到什么目标?你可以自己制作正则表达式。

:

  • 大写(Regex = A-Z)
  • 小写(Regex = a-z)
  • 数字(Regex = 0-9)
  • 特殊字符(Regex =/(斜杠),'s(空格)等)

可以为:A-Za-z0-9然而,regex有很多方法来实现,我不知道你的情况或你想要实现的,但是,我希望你能做点什么与这篇文章:)

试试这个

$output = preg_replace('/[^(A-Za-z's'$¥€)]/u','',$output);

这将删除除A-Z, A-Z,空格和符号$,¥,€以外的任何内容