如果$ _POST包含除文本以外的任何内容

时间:2017-02-22 20:55:52

标签: php if-statement post output contains

我有来自不同页面的帖子数据,然后这个PHP代码处理数据并将其保存在文本文件中。

我希望保存来自$_POST['first']的数据,当且仅当其文本(不包含数字或特殊字符)时

<?php

$myfile = fopen("/var/things.txt", "a+");

$txt = "1st:".$_POST['first'];

fwrite($myfile, $txt);
fclose($myfile);

?>

1 个答案:

答案 0 :(得分:0)

检查它是否有任何特殊字符:

if(preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $_POST['first'])){
   //if string contains special characters.
}

检查字符串是否包含数字:

if(1 === preg_match('~[0-9]~', $_POST['first'])){
   //if string contains special numbers.
}

希望这会对你有帮助!

相关问题