php - strpos and string in string searches -


i have comma delimited string , need able search string instances of given string. use following function:

function ischecked($haystack, $needle) {     $pos = strpos($haystack, $needle);     if ($pos === false) {         return null;     } else {         'return 'checked="checked"';     } } 

example: ischecked('1,2,3,4', '2') searches if 2 in string , ticks appropriate checkbox in 1 of forms.

when comes ischecked('1,3,4,12', '2') though, instead of returning null returns true, finds character 2 within 12.

how should use strpos function in order have correct results?

function ischecked($haystack, $needle) {     $haystack = explode(',', $haystack);     return in_array($needle, $haystack); } 

also can use regular expressions


Comments