jason310771 Posted December 2, 2013 Share Posted December 2, 2013 I can not seem to find out why this code will not work.I am wanting it to first set a dummy string to 'notin' thencheck if any of the strings in the array are in the $fromlink4 variable that has the contents of gethostbyaddr($_SERVER['REMOTE_ADDR']), if just one is set the dummy string $isinarray to 'isin'the next step is to see if $isinarray is set to 'notin' if not then store the visitor details.at the moment it stores everything even if any of the array strings are in $fromlink4 $fromlink4 = $_SERVER['REMOTE_ADDR'] != "" ? gethostbyaddr($_SERVER['REMOTE_ADDR']) : "" . $_SERVER['REMOTE_ADDR'] . ""; $needle = array('bot', 'crawl', 'spider', 'superkabel', 'telenet', 'your-server' ,'whois', 'unassigned', 'fastwebserver', 'opendub', 'sagonet', 'search'); $isinarray = "notin"; //echo("isinarray = false<br>"); foreach($needle as $item){ //echo("<br>item = " . $item . " "); if(strpos("$fromlink4", $item) == true) { // 'false' = IS in string $isinarray = "isin"; //echo("isinarray = true "); } } if($isinarray == "notin") { //echo("<br><br><br><br>isinarray = true add to the DB<br>"); $send_error = "INSERT INTO visitTracker (`data`, `allvars`, `time`, `theip`, `fromlink4`) VALUES ('" . $mysqli->real_escape_string($data) . "', '" . $mysqli->real_escape_string($allvars) . "', '" . $mysqli->real_escape_string($datetimenow) . "', '" . $mysqli->real_escape_string($ipAddress) . "', '" . $mysqli->real_escape_string($fromlink4) . "')"; db_query($mysqli, $send_error); } Link to comment https://forums.phpfreaks.com/topic/284453-how-to-search-string-for-phrase-using-array/ Share on other sites More sharing options...
Barand Posted December 2, 2013 Share Posted December 2, 2013 your code will fail if $fromlink4 starts with one of the items in your array, otherwise it should work. Also, $fromlink4 is already a string so there is no need for the the double quotes round it in the strpos() parameters. A better test is this which will work even if the found string is in position 0. if(strpos($fromlink4, $item) !== false) { Link to comment https://forums.phpfreaks.com/topic/284453-how-to-search-string-for-phrase-using-array/#findComment-1460990 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.