0

I have an orderId which obviously is variable.

I need to compute another 9 digit number based on that.

Example:

$orderId = 100;

$path should be = 0000000100;

----
$orderId = 2350;

$path should be = 0000002000;


--- 
$orderId = 7500;

$path should be = 0000007000;

Thanks a lot in advance!

3
  • ok. And what will be 9 digit number for 7999 according to you? Commented Jul 19, 2016 at 6:56
  • Have you tried anything so far? Leaving that aside, I don't see a clear rule on how to build that. Do you round down if it's x.5? Do you round up if it's bigger than x.5? I can only assume you round down at this point. Commented Jul 19, 2016 at 6:57
  • First off, your examples are 10 instead of 9 digits? So, the general logic is: keep the first digit of orderId, append as many zeros at the end to keep the number of digits from the original and then fill up the front with zeros to reach 9 digits? Commented Jul 19, 2016 at 6:59

1 Answer 1

2

Try like this

 <?php
  $test = strval(7001);
  echo substr_replace(str_pad("",9,"0"),$test[0],(strlen($test)-1)*(-1),0);
?>

Check here : https://eval.in/607614

Another way is

<?php
$test = 658;
$length = 10-strlen($test);
$str_pad = "0000000000";
$str_pad[$length] = strval($test)[0];
echo $final = $str_pad
?>

Check here : https://eval.in/607610

Sign up to request clarification or add additional context in comments.

1 Comment

@DhavalDave : Changed 10 to 9 logic is same

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.