Jump to content


Photo

str_replace problem *** SOLVED ***


  • Please log in to reply
4 replies to this topic

#1 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 27 September 2006 - 03:39 AM

I really hope u guys can help me on this one  ???.

$match = str_replace("*", '', $string);
$match = strlen($match);

if ($match == 0) {
return False; 
} else {
return True;
}

From the code you can see the problem clearly that if no *** are present in the $string,  $match = strlen($match) also returns 0 which does not work for me. How do i change this code to make sure that $match = 0 only when *** are present in the $string and replaced.

Thanks.

#2 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 27 September 2006 - 03:45 AM

Try putting speechmarks around the 0

ie

$match = str_replace("*", '', $string);
$match = strlen($match);

if ($match == "0") {
return False; 
} else {
return True;
}

If you're not part of the solution, you're part of the precipitate

#3 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 27 September 2006 - 03:55 AM

thanks for the tip...but why the " "

#4 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 27 September 2006 - 04:02 AM

Actually, after reading this again i think I have the wrong end of the stick

One way to do it would to be to count the string length before and after the string replace, and have the "false" returned only if there is a difference in those two numbers and the second one comes back as 0

eg
$count1 = strlen($string);
$match = str_replace("*", '', $string);
$match = strlen($match);

if ($match == 0 AND ($count1 != $match)) {
return False; 
} else {
return True;
}

Is that more what you were after?
If you're not part of the solution, you're part of the precipitate

#5 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 27 September 2006 - 04:11 AM

i think this one is solved thanks




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users