Can I use substr() as the needle in in_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
.
精彩评论