gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-10-31 23:34:15 25 4
gpt4 key购买 nike

我正在尝试从用户输入中删除所有非键盘字符,而且我正在努力实现它。以下正则表达式不会替换多个字符,只会替换一个...我尝试添加 /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 />

最佳答案

抱歉,我在第一个回答中混淆了一些东西。

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

及其工作。

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

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

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

关于php - 正则表达式删除非键盘字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6996815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com