how would i do this?!

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
DynamiteHost
Forum Commoner
Posts: 69
Joined: Sat Aug 10, 2002 5:33 pm

how would i do this?!

Post 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 :)
codewarrior
Forum Commoner
Posts: 81
Joined: Wed Aug 07, 2002 1:28 pm

Post by codewarrior »

Err assign it a value? That's what we do in C++, I believe that holds true in PHP also! :lol:
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

try

Code: Select all

if(strpos($number, "0") != "1"){
 $number = "0".$number;
}
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post 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;
}
?>
DynamiteHost
Forum Commoner
Posts: 69
Joined: Sat Aug 10, 2002 5:33 pm

Post by DynamiteHost »

Are there any advantages of using the regular expression over the code hob_goblin gave?
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post 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);
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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
DynamiteHost
Forum Commoner
Posts: 69
Joined: Sat Aug 10, 2002 5:33 pm

Post by DynamiteHost »

Thanks, i've tried hob_goblins method and it seems to work well :wink:
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post 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
Post Reply