2011-08-09 27 views
4

ユーザの入力からキーボード以外の文字をすべて削除しようとしていますが、これを実現するために苦労しています。次の正規表現は、複数の文字を置き換えるものではなく、ただ1つ... /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); 

とその作業にこのコードを試してみました。

何らかの理由で、何らかの理由で2回エスケープする必要がある場合は、パターン内の2つ目のバックスラッシュをすべて削除する必要はありません。

文字クラスをちょっときれいに整理しました。大部分の文字は、文字クラス内にあるときにエスケープする必要はなく、そのようなクラス内であっても特別な意味を持つ文字だけがエスケープされる必要があります。 /正規表現の区切り文字として使用される場合'文字列区切り文字として使用される場合

最後に、コードを変更しても、エスケープを二重にするだけです。

+0

は、残念ながら、その関数が存在しませんPHP ...あなたはpreg_match_allを考えていましたか? – Webnet

+0

@Webnetはい私は何かを混ぜた – stema

関連する問題