0

如何检查字符串中是否有特定的工作?假设我有一个这样的字符串

错误=姓名&密码&邮箱

所以我想检查姓名、通行证或/和电子邮件是否在字符串中。我需要答案是布尔值,所以我可以在那里做一些事情。

4

5 回答 5

2
<?php
$mystring = 'wrong=name&pass&email';
$findme   = 'name';
$pos = strpos($mystring, $findme);


if ($pos === false) {
echo "The string '$findme' was not found in the string '$mystring'";
} else {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
}
?> 
于 2012-08-07T07:09:01.663 回答
1
if ( stristr( $string, $string_im_looking_for) ){
     echo 'Yep!';
}
于 2012-08-07T07:04:24.643 回答
1

采用strstr()

if (strstr($string,'pass'))
{
    echo"pass is here";
}
于 2012-08-07T07:04:31.620 回答
0

你可以先把弦炸开。像这样的东西;

$arrayOfWords = explode('&', $yourString);

然后循环遍历数组并检查 isset。

于 2012-08-07T07:04:16.620 回答
0

从您的示例的外观看来,您实际上想要做的是解析查询字符串,例如parse_str

parse_str($string, $result);
if(isset($result['name']))
  // Do something

但是,如果字符串可能格式错误等。我建议使用strpos, 与strstr其他字符串不同,它不需要创建新字符串。

// Note the `!==` - strpos may return `0`, meaning the word is there at
// the 0th position, however `0 == false` so the `if` statement would fail
// otherwise.
if(strpos($string, 'email') !== false)
  // Do something
于 2012-08-07T07:12:45.083 回答