how to add zeros to string in php [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have strings: 23-65, 123-45, 2-5435, 345-4
I want to add zeros to them so all of them will look like ###-#### (three digits dash four digits): 023-0065, 123-0045, 002-5435, 345-0004
How can i do it in php?
Thanks!

You will need to split them using
$parts = explode('-', $number);`
then use str_pad function:
$parts[0] = str_pad($parts[0], 3, "0");
$parts[1] = str_pad($parts[0], 4, "0");
and then concatenate them again
$number = implode('-', $parts);
Alternatively you can pad them using vsprintf:
$number = vsprintf('%03d-%04d', $parts);

Try:
$str = "23-65, 123-45, 2-5435, 345-4";
$numArray = explode(",",$str);
$str_new = "";
foreach($numArray as $nums) {
$nums = explode("-",$nums);
$num1 = str_pad($nums[0], 3, '0', STR_PAD_LEFT);
$num2 = str_pad($nums[1], 4, '0', STR_PAD_LEFT);
$str_new .= $num1."-".$num2.",";
}
$str_new = rtrim($str_new,",");
Output:
023-0065, 123-0045,0 2-5435, 345-0004

Related

How to remove a specific number from string in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How can I remove a specific number from string, if the number is repeated I just need to remove it one time.
$string = "1 3 5 15 5";
//Remove 5
$valuetoremove = "5";
$newvalue = "1 3 15 5";
I tried
str_replace but this remove all the numbers repeated.
str_replace("5"," ","1 3 5 15 5");
$newvalue = "1 3";
If you specifically want to just remove it one time, then I guess this is what you're after
$string = "1 3 5 15 5";
$valueToRemove = "5";
$exploded = explode(" ", $string);
// $exploded = ["1", "3", "5", "15", "5"];
$key = array_search($valueToRemove, $exploded, true);
if ($key !== false) {
unset($exploded[$key]);
}
$newValue = implode(" ", $exploded);

how to use biasedNumberBetween faker? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to get a number between 1 to 6 with more chances to be close to 1.
i have tried this:
<li>{{Faker\Factory::create()->biasedNumberBetween($min = 10, $max = 20, $function = 'unbiased')}}</li>
What i am trying to do is to generate a number from 1 to 6 rand(1,6); but make the numbers be closer to one as the lower numbers will have more weight than the others.
Something like this ?
<?php
function weightedRand($min, $max, $weightedMax) {
$arr = array();
for($i = 0; $i < 10; $i++) {
$arr[] = rand($min, $weightedMax);
}
$arr[] = rand($min, $max);
return $arr[rand(0,10)];
}
echo weightedRand(1,6, 3);
?>
numbers below 4 will now be more likely than numbers above :)

insert comma into string in php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
how do i insert a comma in string like
$str = "0470 06102009 2981485GIR ADE TAUHID";
every length i put in array like
$legth = array(7,8,15,50);
i just wanna make the result like
0470 ,06102009 ,2981485GIR ,ADE TAUHID
where every string splited according to length on the array, including whitespace,
length(7),length(8),length(15),length(50)
how i do that ?
I would do something like this:
$offset = 0;
$result = implode(",",array_map(function($length) use ($str,&$offset) {
$part = substr($str,$offset,$length);
$offset += $length;
return $part;
},$legth));
Demo: http://ideone.com/ISWZuO
Please note that the output of the demo is what you "should" get based on the input you provided. Your input is wrong for the output you say you want - adjust $legth as needed.
if you are sure of the number of spaces and you want to keep them, you can use substr
// substr( your string, start, length )
substr($str , 0, 7)
This will work for you:
$oldString = "0470 06102009 2981485GIR ADE TAUHID";
$newstring = implode(", ", preg_split("/[\s]+/", $oldString));
echo $newstring;
EDIT:
If you need output on the basis of your array. Then I think you need to modify your array first. And apply below code:
$legth = array(7,8,15,50);
$str = "0470 06102009 2981485GIR ADE TAUHID";
$first = 0;
foreach($legth as $l){
echo substr($str , $first, $l)."<br />";
$first = $first + $l;
}

How To Display Random Results in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Im trying to display 2 or more unique random results from a text file, How do I do that please?
But I need unique results and not 2 or more of the same.
Currently using this code, but it only returns 1 random result:
<?php
$text = file_get_contents('flatFileDB.txt');
$textArray = explode("\n", $text);
$randArrayIndexNum = array_rand($textArray);
$randPhrase = $textArray[$randArrayIndexNum];
?>
<?php echo $randPhrase ?>
I would use something like that
shuffle($textArray);
echo $textArray[0];
echo $textArray[1];
http://php.net/manual/tr/function.shuffle.php
You can give a shot to this also. Code is not tested. I am collecting the used into an array, and check, is that used before.
$text = file_get_contents('flatFileDB.txt');
$textArray = explode("\n", $text);
$used = array();
$countOfRandoms = 2;
$randoms = array();
$i = 1;
do {
if ($countOfRandoms == $i) {
break;
}
$randArrayIndexNum = array_rand($textArray);
if (in_array($randArrayIndexNum, $used)) {
continue;
}
$used[] = $randArrayIndexNum;
$random = $textArray[$randArrayIndexNum];
$i++;
} while (true);

Get part of string in PHP without explode [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have the following string
$s = "hellomyname";
$result = ...
How can I get the "my" out of this the fastest way possible?
You have to find the position of ((my)) in your string with strpos() function and then fetch the word with substr().
here is an example:
<?php
$s1 = 'laldkfjamydnadjacv zdvzkv';
$pos = strpos($s1, 'my');
echo 'First Position---> ' . $pos;
$my1 = substr($s1, $pos, 2);
echo '<br /> this is result---> ' . $my1;
//second test
$s2 = 'hellomyname';
$pos2 = strpos($s2, 'my');
echo '<br />Second position---> ' . $pos2;
$my2 = substr($s2, $pos2, 2);
echo '<br />this is second result---> ' . $my2;
?>
and this is result:
First Position---> 8
this is result---> my
Second position---> 5
this is second result---> my
I think you're looking for substr($s, 5, 2).
Explode (Delimiter, text)
In your case can use:
$result = explode('my', 'hellomyname'); // array([0] => 'hello', [1] => 'name');
If you need get the last value you can put end():
$result = end(explode('my', 'hellomyname')); //name
Reference
http://www.php.net/manual/pt_BR/function.explode.php
http://us1.php.net/end

Categories