如何检查字符串是否包含'@'and '.'?

how to check whether string contains '@ 'and '.'?

search_field : ________

在这些字段中,我们可以输入姓名或电子邮件。考虑如果字符串包含 @.,它是否会被视为 email else name.

我试过的代码是:

if(preg_match('[@|.]', $search_field )) {
    echo "email present";
} 
else{
  echo name present";
}

我输入的格式是:

search_field : masn@

它给了我作为电子邮件存在的输出,但我希望另一个 way.It 中的输出应该显示电子邮件存在,并且仅当它同时包含 @.[=18 时=]

如果您只想验证电子邮件,请试试这个

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  $emailErr = "Invalid email format"; 
}

如果要验证名称,请使用

if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
  $nameErr = "Only letters and white space allowed"; 
}

试试这个代码

if(preg_match('!@+!', $search_field )) {
    echo "email present";
} 
else{
  echo "name present";
}