开发者

Regex to remove non-keyboard characters

I'm trying to remove all non-keyboard characters from a user's input and I'm struggling to make it happen. The following regex isn't replacing multiple characters, only one... I've tried adding /ig on the end instead of /i, but it breaks as an unrecognized flag. I've also tried to use * and various other combinations and can't seem to figure it out.

$data = preg_replace('/[^a-z0-9<>\s\!\@\#\$\%\^\&\*\(\)\_\+\-\=\{\}\[\]\|\\\:\;\"\'\?\/\>\.\<\,\®]/i', '', $data);

Test against this text:

Nutritional counseling <br /><br />Online Clinic Visits except as specifically covered in the Certificate or EOC. ÂÂ<br /><br />Health club memberships <br /><br />Any services to the extent you are entitled to receive Medicare benefits for those services without payment of additional premium for Medicare coverage <br /><br />Food or dietary supplements, except, as specifically stated in the certificate or EOC or as required by law <br /><br />Genetic testing for nonmedical reasons or when there is no medical indication or no family history of genetic abnormality <br /><br />Outdoor treatment programs <br /><br />Replacement of prosthetics and durable medical equipment when lost or stolen <br /><br />Any services or supplies provided to any person not covered under the Agreement in connection wi开发者_运维百科th a surrogate pregnancy <br /><br />Immunizations solely for travel outside the United States <br /><br />Services or supplies related to a pre-existing condition <br /><br />Educational services except as specifically provided or arranged by Anthem Blue Cross <br /><br />Infertility services (including sterilization reversal and costs associated with the storage of sperm, eggs, embryos and ovarian tissue) except as specifically stated in the certificate or EOC <br /><br />Care or treatment provided in a noncontracting hospital <br /><br />Private duty nursing except as specifically stated in the certificate or EOC <br /><br />Services primarily for weight reduction except medically necessary treatment of morbid obesity <br /><br />Outpatient drugs, medications or other substances dispensed or administered in any outpatient setting <br /><br />Contraceptive devices unless your physician determines that oral contraceptive drugs are not medically appropriate. <br /><br />Vein Treatment: Treatment of varicose veins or telangiectatic dermal veins (spider veins) by anyÂÂ method (including sclerotherapy or other surgeries) when services are rendered for cosmetic purposes. <br /><strong><br />Third Party Liability</strong> - Anthem Blue Cross is entitled to reimbursement of benefits paid if the member recovers damages from a legally liable third party. <br /><br /><strong>Coordination of Benefits</strong> - The benefits of this plan may be reduced if the member has any other group health, dental, prescription drug or vision coverage so that the services received from all group coverages do not exceed 100% of the covered expense.<br />


Sorry I mixed something up in my first answer.

PHP does not know the g modifier. If you just want to match it use preg_match_all instead of preg_match, but preg_replace is greedy by default.

I tried this code on writecodeonline.com

$data = "Nutritional counseling <br /><br />Online ÄÖÜ Clinic Visits except as specifically covered in the Certificate or EOC. ÂÂ<br /><br />";
$pattern = '/[^a-z0-9_<>\\s!@#$%^&*()+={}\\[\\]|\\/:;"\\'?.,®-]/i';

$data = preg_replace($pattern, '', $data);
echo ($data);

and its working.

For some reason it needs double escaping there, if you don't need it just remove every second back slash in the pattern.

I cleaned your character class a little bit up. Most of the characters don't need to be escaped when they are inside a character class, only those which have a special meaning even inside such a class e.g. / when used as regex delimiter, ' when used as string delimiter ...

But at the end even your code works there, when I just change your code to double escaping.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜