2011-08-09 2 views
4

사용자의 입력에서 키보드가 아닌 문자를 모두 제거하려고 시도하고 있습니다. 다음 정규식은 복수 문자 하나만 바꾸는 것이 아닙니다 ... /i 대신 끝에 /ig을 추가하려고했지만 인식 할 수없는 플래그로 손상됩니다. 나는 또한 * 및 다양한 다른 조합을 사용하려고 시도했는데 그것을 파악하는 것처럼 보일 수 없습니다. 이 텍스트에 대한키보드가 아닌 문자를 제거하는 정규식

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

+8

용서해주십시오. 키보드로 모든 문자를 입력 할 수 있습니까? – ajreal

+4

아마 영숫자가 아닌 문자를 의미할까요? –

+1

가능한 복제본 : http://stackoverflow.com/questions/1401317/remove-non-utf8-characters-from-string – piddl0r

답변

2

미안 내 첫 번째 대답에 뭔가를 혼합.

PHP는 g 수정자를 알지 못합니다. 일치시키려는 경우 preg_match 대신 preg_match_all을 사용해야하지만 기본적으로 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_<>\\[email protected]#$%^&*()+={}\\[\\]|\\/:;"\\'?.,®-]/i'; 

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

와의 작업에이 코드를 시도했다.

몇 가지 이유로 두 번 이스케이프해야합니다. 필요하지 않으면 패턴의 모든 두 번째 백 슬래시를 제거하면됩니다.

문자 클래스를 약간 청소했습니다. 대부분의 문자는 문자 클래스 내에있을 때 이스케이프 할 필요가 없으며, 그러한 클래스 내부에서도 특별한 의미를 갖는 문자 만 이스케이프 할 필요가 없습니다. /을 정규 표현식 구분 기호로 사용하는 경우 '을 문자열 구분 기호로 사용하면 ...

마지막으로 코드를 두 번 이스케이프 처리하도록 변경하면 결국 코드가 작동합니다.

+0

불행히도 그 기능은 존재하지 않는다 PHP ... 당신은 preg_match_all을 생각하고 있었습니까? – Webnet

+0

@Webnet 예 나는 뭔가를 섞었다. – stema

관련 문제