正则表达式删除非键盘字符

时间:2011-08-09 13:20:05

标签: php regex

我正在尝试从用户的输入中删除所有非键盘字符,我正在努力实现它。以下正则表达式不会替换多个字符,只有一个...我尝试在末尾添加/ig而不是/i,但它会作为无法识别的标记而中断。我也尝试使用*和其他各种组合,似乎无法弄明白。

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

针对此文本进行测试:

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

1 个答案:

答案 0 :(得分:2)

抱歉,我在第一个答案中混淆了一些内容。

PHP不知道g修饰符。如果您只想匹配它,请使用preg_match_all代替preg_match,但默认情况下preg_replace会贪婪。

我在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);

及其工作。

出于某种原因,它需要在那里进行双重转义,如果你不需要它,只需删除模式中的每一秒反斜杠。

我清理了你的角色课程。大多数角色在角色类内部时不需要被转义,只有那些在这样的类中具有特殊含义的角色,例如用作正则表达式分隔符时/'用作字符串分隔符时...

但最后,即使你的代码在那里工作,当我只是将你的代码更改为双重转义时。

相关问题