开发者

Can I use substr() as the needle in in_array()?

开发者 https://www.devze.com 2023-01-12 01:48 出处:网络
$haystack = \'I am a haystack. Hear me rawr.\'; $pos = strlen($haystack); $nlen = 1; $needle = array(\'.\', \'. \');
$haystack = 'I am a haystack. Hear me rawr.';
$pos = strlen($haystack);
$nlen = 1;

$needle = array('.', '. ');

print_r(in_array(substr($haystack, $pos, $nlen), $needle, true));

I am having trouble figuring out why this is failing. I am trying to see if an 开发者_JAVA百科array of needles matches the result that substr chooses from the haystack? How can I return that value as boolean?


Yes & NO because substr returns a string which is needle in your case and FALSE on failure in which case it won't be a valid argument to in_array function.

You should first extract a part of string using substr and need to make sure that you extracted some string and it did not return FALSE, only then you should use it in in_array.

0

精彩评论

暂无评论...
验证码 换一张
取 消