0

SQL SELECT クエリを実行する前に、PHP 検索フォームでユーザー入力から特殊文字を削除する方法を見つけようとしています。

ユーザー入力を選択クエリ> mysql 5> Linuxに送信するPHP検索フォームがあります。選択クエリは、LIKE 一致の 1 つの列を調べ、結果を結果ページに戻します。データベースの列には特殊文字はなく、英数字のみです。

私のデータベースフィールド値は次のようになります 33345678DEP

ユーザーが 333-456*78DEP と入力した場合、特殊文字を削除するにはどうすればよいですか?

私はREGEXについて読みました - それが答えかもしれませんが、それを実装する方法がわかりません。

検索を実行するMY PHPコードは次のとおりです。

if (isset($_GET['InventoryByVendorForm'])) {
$colname_InventoryByVendorResults = $_GET['InventoryByVendorForm'];
}
mysql_select_db($database_ic3, $ic3);
$query_InventoryByVendorResults = sprintf("SELECT icCombinedSupplier_wrk.icMaster_icmKEY, icCombinedSupplier_wrk.icsSupplierID, icCombinedSupplier_wrk.icsPartNum, icCombinedSupplier_wrk.icsQuantityOnHand, icCombinedSupplier_wrk.icsCost, icCombinedSupplier_wrk.icsCore, icCombinedSupplier_wrk.icsLastInventoryUpdate, icMaster.icmLineCode, icAttributes.`icaPartslink#` AS icaPartslink FROM icMaster INNER JOIN (icCombinedSupplier_wrk INNER JOIN icAttributes ON icCombinedSupplier_wrk.icMaster_icmKEY = icAttributes.icMaster_icmKEY) ON icMaster.icmKEY = icCombinedSupplier_wrk.icMaster_icmKEY WHERE `icCombinedSupplier_wrk`.`icMaster_icmKEY` Like %s ORDER BY icMaster_icmKEY ASC", GetSQLValueString("%" . $colname_InventoryByVendorResults . "%", "text"));

私の最善の推測では、ステートメントの周りに REGEX [[alnum]] の使用を実装する必要があると思います。

$colname_InventoryByVendorResults = $_GET['InventoryByVendorForm']

検索ボックスのユーザー入力から英数字以外のすべての文字を削除するにはどうすればよいですか?

4

1 に答える 1

1

preg_replaceは、これを行うための適切なツールです。

$validated = preg_replace("/[^A-Za-z0-9]/","",$colname_InventoryByVendorResults);

英数字セットのショートカットがありますが、この方法では非常に直感的に理解できます。

「^」は選択を無効にするため、英数字でない文字は空の文字列に置き換えられます。

良い一日を、ステファン

于 2012-04-22T14:58:02.540 に答える