如何使用PHP删除除$(美元符号)以外的所有特殊字符?

How to remove all special char except $ (dollar sign) using php ?

I tried use this

$string = preg_replace("/[^ \w]+/", "", $string);

But all special char include $ (dollar sign) was remove

i tried this too $string = preg_replace("/$[^ \w]+/", "", $string);

But not work.

I want to store $ (dollar sign) , How can i do that ?

You're accepting so few characters that you could just "spell them out": everything that is not A-Z0-9$ -> replace.

<?php
$input= '_~!@#$%^&*()+  babnQWWWEWQEJ';
echo preg_replace('/[^A-Z0-9$]+/', '', $input);