note 94469 modified in function.strpos by danbrown

From: Date: Fri, 06 Nov 2009 22:06:44 +0000
Subject: note 94469 modified in function.strpos by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-162465@lists.php.net to get a copy of this message
A function I made to find the first occurrence of a particular needle not enclosed in quotes(single or double). Works for simple nesting (no backslashed nesting allowed). <?php function strposq($haystack, $needle, $offset = 0){ $len = strlen($haystack); $charlen = strlen($needle); $flag1 = false; $flag2 = false; for($i = $offset; $i < $len; $i++){ if(substr($haystack, $i, 1) == "'"){ $flag1 = !$flag1 && !$flag2 ? true : false; } if(substr($haystack, $i, 1) == '"'){ $flag2 = !$flag1 && !$flag2 ? true : false; } if(substr($haystack, $i, $charlen) == $needle && !$flag1 && !$flag2){ return $i; } } return false; } echo strposq("he'llo'character;\"'som\"e;crap", ";"); //16 ?> --was-- A function I made to find the first occurrence of a particular needle not enclosed in quotes(single or double). Works for simple nesting (no backslashed nesting allowed). function strposq($haystack, $needle, $offset = 0){ $len = strlen($haystack); $charlen = strlen($needle); $flag1 = false; $flag2 = false; for($i = $offset; $i < $len; $i++){ if(substr($haystack, $i, 1) == "'"){ $flag1 = !$flag1 && !$flag2 ? true : false; } if(substr($haystack, $i, 1) == '"'){ $flag2 = !$flag1 && !$flag2 ? true : false; } if(substr($haystack, $i, $charlen) == $needle && !$flag1 && !$flag2){ return $i; } } return false; } echo strposq("he'llo'character;\"'som\"e;crap", ";"); //16 http://php.net/manual/en/function.strpos.php

« previous php.notes (#162465) next »