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

Here is my code in php

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

Here I want to keep any currency symbols as it is and need to remove junk characters .

What changes should I make in regex for this issue ?

Thanks in advance

Answers


You need to add the u modifier to the RegEx and it works nicely:

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

Outputs

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

First question, what do you want to achive? Regex can you make by yourself.

Like:

  • Uppercase (Regex = A-Z)
  • Lowercase (Regex = a-z)
  • Numbers (Regex = 0-9)
  • Special chars (Regex = /(slash), \s (space) etc)

To totalize this can be: A-Za-z0-9 However, regex has to many ways to implement, i have no idea about your case or what you want to achive, however, i hope you can do something with this post :)


Try this

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

This will strip out anything other than A-Z, a-z, blank space and symbols $,¥,€


Need Your Help

return Identity in SQLServer Compact

c# sql-server compact-framework sql-server-ce identity-column

How do you get the identity column value after an insert in SQL Server Compact 3.5?

Error with axShockwaveFlash in C#

c# activex

I'm using axShockwaveFlash in my C# project.