Regex to remove characters without keyboard

I am trying to remove all characters without a keyboard from user input, and I am trying my best to make this happen. The following regular expression does not replace several characters, only one ... I tried adding /ig to the end instead of /i , but it breaks like an unrecognized flag. I also tried to use * and various other combinations and cannot figure out how to understand this.

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

Test 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 with 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 />

+4
source share
1 answer

Sorry, I put something 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 his work.

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

I cleaned up the character class a bit. Most characters need not be avoided when they are inside a character class, only those that have special meaning even inside such a class, for example. / when used as a regex separator, ' when used as a line separator ...

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

+2
source

All Articles