the_oliver Posted June 25, 2007 Share Posted June 25, 2007 Hello, Im trying to search for a charictor in a string using the following. $arr = array("\"","<",">"); if (strpos($field, $arr) === false) { } if i specify a charictor instaead of using $arr it works fine, however if i use and array ($arr) it won't work. I need to search for more then one charictor! Can anyone tell me a good way round this? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/57083-solved-using-array-in-strpos/ Share on other sites More sharing options...
Wildbug Posted June 25, 2007 Share Posted June 25, 2007 strpos() doesn't accept an array as a parameter. Use another function to search for multiple "charictors" or use two separate calls to strpos(). if (strpos(...) or strpos(...)) { ...etc } Quote Link to comment https://forums.phpfreaks.com/topic/57083-solved-using-array-in-strpos/#findComment-282099 Share on other sites More sharing options...
per1os Posted June 25, 2007 Share Posted June 25, 2007 You could create your own little function to handle it. <?php function my_strpos($haystack, $needle) { if (is_array($needle)) { foreach ($needle as $need) { if (strpos($haystack, $need) !== false) { return true; } } }else { if (strpos($haystack, $need) !== false) { return true; } } return false; } ?> http://www.php.net/strpos Description int strpos ( string $haystack, mixed $needle [, int $offset] ) needle If needle is not a string, it is converted to an integer and applied as the ordinal value of a character It does not accept an array as the needle. An int or a string. Quote Link to comment https://forums.phpfreaks.com/topic/57083-solved-using-array-in-strpos/#findComment-282101 Share on other sites More sharing options...
sasa Posted June 25, 2007 Share Posted June 25, 2007 try <?php $field ='<sasa>'; if (preg_match('/["<>]/', $field)) {echo 'yes';} else echo 'no'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/57083-solved-using-array-in-strpos/#findComment-282105 Share on other sites More sharing options...
the_oliver Posted June 25, 2007 Author Share Posted June 25, 2007 frost110's solution worked perfectly! Many thanks! Quote Link to comment https://forums.phpfreaks.com/topic/57083-solved-using-array-in-strpos/#findComment-282153 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.