what is wrong with that?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

what is wrong with that?

Post by yaron »

Hello all,
I'm trying to read an array and my purpose is whenever I see a value which is all ***** (between 30-100) I want to do store this value.
here is what I got:
function isStar($string)
{
if(preg_match("/^\*{30,100}\*$/",$string))
return true;
else
return false;
}
for ($i=0;$i<count($allArr);$i++)
{
$aLine=$allArr[$i];
if(isStar($aLine))
{
if(isStar( $allArr[$i+1]) || isStar($allArr[$i+2])) || isStar($allArr[$i+3])) )
/*do something*/
}
}
it keeps getting false answeres and never true though it should!
any ideas?
thanks
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

Post by yaron »

well, I found what is the problem but I still don't know how to solve it.
whenever I read a value I use the trim function to remove pre and post whitesapces.
I need to use trim for other purpose but when I use it the isStar function returns false no matter what!
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

you seem to have some basic structure errors in your code, i have ironed out what i can see

Code: Select all

function isStar($string) { 
	if(preg_match("/^\*{30,100}\*$/",$string)) {
		return true; 
	} else { 
		return false; 
	} 
}

for ($i=0;$i<count($allArr);$i++) { 
	$aLine=$allArr[$i]; 
	if(isStar($aLine)) { 
		if(isStar( $allArr[$i+1]) || isStar($allArr[$i+2])) || isStar($allArr[$i+3])) ) {
			/*do something*/ 
		} 
	} 
}
Mark
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

Post by yaron »

my problem is with mu isStar function.
is there a better way to determine oif a string contains between 30-100 stars and nothing else?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

try replacing your function with this

Code: Select all

function isStar($string) { 
   
   if (substr_count($string, "*") >= 30 && substr_count($string, "*") <= 100) {
      return true; 
   } else { 
      return false; 
   } 
}
Mark
Post Reply