note 17261 modified in function.strpos by zak

From: Date: Sun, 02 Dec 2001 21:56:45 +0000
Subject: note 17261 modified in function.strpos by zak
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-21540@lists.php.net to get a copy of this message
To: clambert@gamespy.com After trying your routine, I realised that it wouldn't work either. First of all, it doesn't take into account the particularities of strpos and will return "1" regardless of wherther or not the string was found. Plus, if your needle is larger than the haystack, you will get an offset error. This one is tested, try it if you like. ($input like $needle, $search like $haystack) function find_string ($input, $search) { while (is_integer($offset=strpos($search,$input,$offset))) $offset+=strlen($input); $found++;} return $found; } ---------- Mickael Nadeau, President Pure Media Productions (418) 527-2155 ---------- admin@puremedia.ca http://www.puremedia.ca/ ---------- --was-- To: clambert@gamespy.com After trying your routine, I realised that it wouldn't work either. First of all, it doesn't take into account the particularities of strpos and will return "1" regardless of wherther or not the string was found. Plus, if your needle is larger than the haystack, you will get an offset error. This one is tested, try it if you like. ($input like $needle, $search like $haystack) function find_string ($input, $search) { while (is_integer(strpos($search,$input,$offset))) { $offset+=strlen($input); $found++;} return $found; } ---------- Mickael Nadeau, President Pure Media Productions (418) 527-2155 ---------- admin@puremedia.ca http://www.puremedia.ca/ ---------- http://www.php.net/manual/en/function.strpos.php

« previous php.notes (#21540) next »