特定の作業が文字列で利用可能かどうかを確認するにはどうすればよいですか?このような文字列があるとしましょう
間違った=名前&パス&メール
名前、パス、または電子メールが文字列に含まれているかどうかを確認したいと思います。答えはブール値にする必要があるので、そこでいくつかのことができます。
特定の作業が文字列で利用可能かどうかを確認するにはどうすればよいですか?このような文字列があるとしましょう
間違った=名前&パス&メール
名前、パス、または電子メールが文字列に含まれているかどうかを確認したいと思います。答えはブール値にする必要があるので、そこでいくつかのことができます。
<?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";
}
?>
if ( stristr( $string, $string_im_looking_for) ){
echo 'Yep!';
}
使用するstrstr()
if (strstr($string,'pass'))
{
echo"pass is here";
}
最初に文字列を分解できます。このようなもの;
$arrayOfWords = explode('&', $yourString);
次に、配列をループしてissetを確認します。
あなたの例の外観から、実際にやりたいことは、たとえば次のようにクエリ文字列を解析することです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