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 want to split 4113.52318N in two parts like
41 and 13.52318N
And after that I want to remove N from the second value.
any help please?
There are several ways to do this, one is with preg_match_all, i.e.:
<?php
$string = "4113.52318N";
$result = preg_match_all('/^(\d{2})([\d.]+)/', $string, $matches);
$partOne = $matches[1][0]; //41
$partTwo = $matches[2][0]; //13.52318
Ideone Demo
Try this :
$part1 = substr('4113.52318N', 0, 2) // 41
$part2 = substr('4113.52318N', 3); // 13.52318N
$final = substr('4113.52318N', 0, -1); // 4113.52318
Use substr: http://php.net/manual/en/function.substr.php
$original = "4113.52318N";
$fortyone = substr($original, 0, 2); // 41
$other = substr($original, 3); // 13.52318N
$n_removed = substr($other, 0, -1); // 13.52318
Related
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 1 year ago.
Improve this question
How can I format a string to prepend UTC if it starts with + or - and add a : before the last two digits if it ends with 4 digits?
Examples and expected result:
PST > PST
+08 > UTC+08
-0845 > UTC-08:45
Thank you!
<?php
$string = "PST";
if (substr($string, 0, 1) === '+' || substr($string, 0, 1) === '-'){
if(strlen($string) == 3){
$newString = 'UTC'.$string;
}
else{
$newString = 'UTC'. substr($string, 0, 3). ':' .substr($string, -2, 2);
}
}
else{
$newString = $string;
}
echo $newString;
You don't need regex for that or at least regex seems overkill. You can simple create this logic using an if-else and modify your string accordingly
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 2 years ago.
Improve this question
I want to convert int into comma separated string in php.
for example,
100000 into "1,00,000".
you can use number_format
$number=number_format(100000);
$echo $number; //output 100,000
or
$number=number_format(100000);
$newNumber=sprintf('"%s"',$number);
echo $newNumber ; //output "100,000"
you can use number_format() function
<?php
$number = '100000.457888';
echo number_format($number, 0); // returns 100,000
echo number_format($number, 1); // returns 100,000.5
echo number_format($number, 2); // returns 100,000.46
echo number_format($number, 3); // returns 100,000.458
echo number_format($number, 4); // returns 100,000.4579
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 5 years ago.
Improve this question
Hi am having 10 elements in array . Am trying to get my random element mostly from first 5 elements. Which means random element appearance from first 5 elements should be much greater than next 5 elements
$arr = array('a','b','c','d','e','f','g','h','i','j');
$random = $arr[array_rand($arr)];
Am using above one to get the random element normally
Use function rand(min_num, max_num):
function rand5($array) {
$part = rand(1, 10);
return ($part > 3) ? $array[rand(0, 4)] : $array[rand(5, 9)];
}
$arr = array('a','b','c','d','e','f','g','h','i','j');
$random = rand5($arr);
Try this simple and easy code :-
$arr = array('a','b','c','d','e','f','g','h','i','j');
$rand = rand(0,9);
echo $arr[($rand <= 6 ? ($rand%5) : $rand)];
You just have to get the random number between 0 to 9 and divide that number in 70%(0-6) and 30%(7-9). If its greater than 5 then only use the remainder else directly get that number
Here is the fiddle :- https://3v4l.org/TWGJJ
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 need to create an array of integers from strings of text composed of the integers separated by whitespace and plus signs, for example
$string = "1 + 2 + 3 + 4";
is extracted into
$array = ('1' , '2' ,'3' , '4');
This needs to be done in php.
Thank you in advance for any help.
Thankyou for the question refinment.
<?php
$string = '1 + 2 + 3';
$array = explode(' + ', $string);
for ($a=0;$a<count($array);$a++){
$array[$a] = (int) $array[$a]; // RECAST STRING TO INTEGER
}
?>
To get the array you need to use PHPs explode function, which will split a string into an array by a delimiter, for example:
$integerString = '1 + 2 + 3 + 4';
$integerArray = explode(' + ', $integerString);
Use preg_match_all!
<?php
$string = '1 + 2 + 3';
preg_match_all("'[0-9]+'sim", $string, $out);
print_r($out);
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
I want to know how I can randomize first and last two digits in php? Suppose the current code is:
FJA793HIYX
then after randomizing it should be:
HJA793HIGD
How can I do that?
Try
$str = 'HJA793HIGD';
$filter = substr($str,1,7);
echo $newstr= randLetter().$filter.randLetter().randLetter();
function randLetter() {
$int = rand(0,26);
$alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$rand_letter = $alpha[$int];
return $rand_letter;
}
Try this!
$randomDigit = substr( "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ,mt_rand( 0 ,34 ) ,1 ) .substr( md5( time() ), 1)