trink Posted September 3, 2007 Share Posted September 3, 2007 This is SUPPOSED to get the location of the Xth occurance of a character in a string, where $xpos is the occurance that you want to get, and $xchar is the character that you want to get. function strxpos($xtext,$xchar,$xpos){ $pshchar=strpos($xtext,$xchar); $owut=0; for($numon=1;$numon <= $xpos;$numon++){ $result=strpos($xtext,$xchar,$owut); $owut=(strpos($xtext,$xchar,$owut) + 1); } return $result; } It works to an extent. Say that there are only 5 occurances in the string, but the number for $xpos is 7. It will still retrieve a number, which I don't want it to do, for some reason its really wierd, i've tried replacing it with function strxpos($xtext,$xchar,$xpos){ //strxpos('a.b.c.d.e','.',3) returns 5 (first char in string is 0) $nrchars=substr_count($xtext,$xchar); $pshchar=strpos($xtext,$xchar); $owut=0; for($numon=1;$numon <= $xpos || $numon <= $nrchars;$numon++){ $result=strpos($xtext,$xchar,$owut); $owut=(strpos($xtext,$xchar,$owut) + 1); } return $result; } but still it bears no fruit, it just doesn't work at all when I change it to that. Any tips would be greatly appreciated. Thanks, Trink Link to comment https://forums.phpfreaks.com/topic/67719-problem-with-a-function-that-ive-been-working-on-for-a-while/ Share on other sites More sharing options...
btherl Posted September 3, 2007 Share Posted September 3, 2007 Try this: function strxpos($xtext,$xchar,$xpos){ $pshchar=strpos($xtext,$xchar); $owut=0; for($numon=1;$numon <= $xpos;$numon++){ $result=strpos($xtext,$xchar,$owut); if ($result === false) return false; $owut=(strpos($xtext,$xchar,$owut) + 1); } return $result; } It will return false if the requested occurance doesn't exist in the string. Link to comment https://forums.phpfreaks.com/topic/67719-problem-with-a-function-that-ive-been-working-on-for-a-while/#findComment-340240 Share on other sites More sharing options...
trink Posted September 3, 2007 Author Share Posted September 3, 2007 It ran into a loop of death. Basically I've got this: function strxpos($xtext,$xchar,$xpos){ $nrchars=substr_count($xtext,$xchar); $pshchar=strpos($xtext,$xchar); $owut=0; for($numon=1;$numon <= $xpos;$numon++){ $result=strpos($xtext,$xchar,$owut); $owut=(strpos($xtext,$xchar,$owut) + 1); } return $result; } function gettokenbynum($gtok1,$gtok2,$gtok3){ if($gtok2 != counttokens(trim($gtok1,$gtok3),$gtok3)){ if($gtok2 == 1){ return substr(trim($gtok1,$gtok3),0,strpos(trim($gtok1,$gtok3),$gtok3)); }else{ return substr(trim($gtok1,$gtok3),(strxpos(trim($gtok1,$gtok3),$gtok3,($gtok2 - 1)) + 1),strpos(substr(trim($gtok1,$gtok3),(strxpos(trim($gtok1,$gtok3),$gtok3,($gtok2 - 1)) + 1)),$gtok3)); } }else{ return substr(trim($gtok1,$gtok3),(strrpos(trim($gtok1,$gtok3),$gtok3) + 1)); } } And this is the script that calls those functions. (those functions are located in atok.class.php. include 'atok.class.php'; $banhandle=@fopen("bans.txt", "r"); $ipban=@fread($banhandle, @filesize("bans.txt")); @fclose($banhandle); $num=1; while(gettokenbynum(gettokenbynum($ipban,$num,';'),1,'|')){ $xipban=gettokenbynum(gettokenbynum($ipban,$num,';'),1,'|'); $reason=gettokenbynum(gettokenbynum($ipban,$num,';'),2,'|'); $bannum=$num; if(eregi($xipban,$_SERVER['REMOTE_ADDR'])){ die("You have been banned from jukebot by an admin for the reason: ".$reason."<br>Go to IRC on the server irc.deltaanime.net in the channel #punk to appeal for an unban.<br>If you do not have an IRC client installed you can use <a href=\"http://chat.deltaanime.net\" class=\"link\">DAIRC Webchat</a>. (Channel: #punk)<br>WHEN YOU APPEAL FOR AN UNBAN REMEMBER THIS NUMBER (YOUR BAN NUMBER): ".$bannum); } $num++; } And I've run out of ideas, which is actually fairly easy for me to do. Any help again would be greatly appreciated, and although it didn't work, I do appreciate the feedback btherl. Link to comment https://forums.phpfreaks.com/topic/67719-problem-with-a-function-that-ive-been-working-on-for-a-while/#findComment-340242 Share on other sites More sharing options...
btherl Posted September 3, 2007 Share Posted September 3, 2007 You'll need to check if strxpos() succeeded or failed instead of just passing it directly to substr(). That's probably what's causing the looping. You can add some diagnostic print messages to see where the problem is. Link to comment https://forums.phpfreaks.com/topic/67719-problem-with-a-function-that-ive-been-working-on-for-a-while/#findComment-340261 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.