【问题标题】:if $_POST contains anything other than text do如果 $_POST 包含文本以外的任何内容
【发布时间】:2017-07-13 03:06:29
【问题描述】:

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

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

<?php

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

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

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

?>

【问题讨论】:

  • 你有没有花时间在谷歌上搜索这个?如果有,是否少于 30 秒?
  • 你是如何尝试实现你的 想要的??
  • 你有这个表格吗?
  • 你认为什么是特殊字符???
  • @RiggsFolly 你忘了 => ? => “想要吗?”。

标签: php if-statement post output contains


【解决方案1】:

检查是否有特殊字符:

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

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

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

希望对您有所帮助!

【讨论】:

  • 谢谢,这很有帮助!
  • 不客气!您能否检查一下是否已解决?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-09
  • 1970-01-01
相关资源
最近更新 更多