note 79700 deleted from function.substr by nlopess

From: Date: Sun, 09 Dec 2007 10:30:26 +0000
Subject: note 79700 deleted from function.substr by nlopess
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-132969@lists.php.net to get a copy of this message
Note Submitter: cristianlf at cristianlf dot com ---- /*---English---*/ charAt: i needed the position of one char in a String, specifiying the number of occurrence of the char into the string. /* $string --> '12-12-12-23-34-45-5-6-70' $search --> '-' $occurence --> 5 */ function charat($string,$search,$occurence) { $occur = 0; for($i=0; $i<strlen($string); $i++) { if (substr ($string,$i,1) ==$search) { $occur++; if ($occur == $occurence) { return $i; } } } return -1; } echo charat('12-12-12-23-34-45-5-6-70','-',5); //14 echo charat('12-12-12-23-34-45-5-6-70','-',1); //2 echo charat('12-12-12-23-34-45-5-6-70','+',1); //-1 /* maybe the name of the function it is not accurrate, but works fine. */ /*---Espanol ---*/ Necesitaba una funcion que me dijera la posicion de un caracter en una cadena, como es posible que un caracter exista mas de una vez, el tercer parametro es para identificar el numero de ocurrencia que quiero que salga. Cristianlf

« previous php.notes (#132969) next »