Jump to content

problem with a function that i've been working on for a while


Recommended Posts

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

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.

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.