如何删除所有垃圾字符,但在PHP中通过正则表达式保留货币符号?

时间:2014-03-12 09:00:38

标签: php regex utf-8

这是我在php中的代码

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

在这里,我希望保留任何货币符号,并且需要删除垃圾字符。

我应该在正则表达式中针对此问题做出哪些更改?

提前致谢

3 个答案:

答案 0 :(得分:1)

您需要将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.

答案 1 :(得分:0)

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

像:

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

总结这可以是:A-Za-z0-9 然而,正则表达式有很多方法可以实现,我不知道你的情况或你想要实现的目标,但是,我希望你能用这篇文章做点什么:)

答案 2 :(得分:0)

试试这个

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

除了A-Z,a-z,空格和符号$,¥,€

之外,它将除去