Page 1 of 1

how would i do this?!

Posted: Sat Sep 14, 2002 5:07 pm
by DynamiteHost
Say if I had $number, how would I scan it to see if the value started with a "0"? And if it didn't have a 0 at the beginning, how would I add one?

Hope you get my drift :)

Posted: Sat Sep 14, 2002 5:09 pm
by codewarrior
Err assign it a value? That's what we do in C++, I believe that holds true in PHP also! :lol:

Posted: Sat Sep 14, 2002 5:11 pm
by hob_goblin
try

Code: Select all

if(strpos($number, "0") != "1"){
 $number = "0".$number;
}

Posted: Sun Sep 15, 2002 2:56 am
by Takuma
You could use regular expression but it'll be slower:

Code: Select all

<?php
if(!ereg("^ї0]+ї0-9A-Za-z]*$",$string)) {
  $string = "0".$string;
}
?>

Posted: Sun Sep 15, 2002 4:59 am
by DynamiteHost
Are there any advantages of using the regular expression over the code hob_goblin gave?

Posted: Sun Sep 15, 2002 11:12 am
by dusty
not in this case which is why that post makes absolutely no sense.

"^0" is all you needed in that exp

if you're trying to pad your numbers you could use str_pad (i'm not sure if that's what you're trying to get at, so it's just a thought)

Code: Select all

$number = str_pad($number,"2","0",STR_PAD_LEFT);

Posted: Sun Sep 15, 2002 11:18 am
by hob_goblin
I'm pretty sure my example would be the best, as string pad wouldn't check if there was a 0 at the begining already

Posted: Sun Sep 15, 2002 11:20 am
by DynamiteHost
Thanks, i've tried hob_goblins method and it seems to work well :wink:

Posted: Sun Sep 15, 2002 11:57 am
by dusty
i wasn't saying that it wasn't, i was just throwing that idea out incase he is trying to pad the hard way which is why i put that little disclaimer at the end there saying so.

Code: Select all

if($numberї0] != 0) {
  $number = "0$number";
}
w/o using strpos so it's a little faster