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
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 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
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 decompose a time in two part, I mean by that, I want to take the left side and the right side, like a cut off right ?
22:02:00
23:00:00
23:12:00
Imagine those number, it doesn't matter if there's seconds or not, so we can kick them
22:02
23:00
23:12
Now, I to take the separate hour and minute. How can we do that ?
The simplest way would be to just cut out the first 5 characters of the string:
$time = '22:02:00';
echo substr($time, 0, 5); // 22:02
You can also parse the time using e.g. the DateTime class:
$time = '22:02:00';
$parsed = DateTime::createFromFormat('H:i:s', $time);
echo $parsed->format('H:i');
As Mark Baker commented, the strptime() function can also be used:
$time = '22:02:00';
$parsed = strptime($time, '%H:%M');
echo str_pad($parsed['tm_hour'], 2, '0', STR_PAD_LEFT) . ':' . str_pad($parsed['tm_min'], 2, '0', STR_PAD_LEFT);
Regular expressions would also work:
$time = '22:02:00';
preg_match('/^(?P<hour>\d{2}):(?P<minute>\d{2})/', $time, $result);
if (count($result) > 0) {
echo "{$result['hour']}:{$result['minute']}";
}
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 would like to create a website that allows a user to generate a selectable number of unique strings of text that all follow an algorithm but as it is website based I am not too sure about how I go about it.
For example user A wants to generate 20 strings of unique text that all follow say AA***B^^** where A&B is a constant that doesn't change, where * is a random number and ^ is a random letter.
Is that possible? I am thinking of using php rand for the number but not 100% sure.
Thanks
You could use something like this:
<?php
function randomGenerator($string)
{
$string_array = str_split( $string );
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
foreach ($string_array as $k => $v) {
if ($v == '*')
$string_array[$k] = rand(0,9);
if ($v == '^')
$string_array[$k] = $characters[rand(0,51)];
}
$string = implode('', $string_array);
return $string;
}
echo randomGenerator('AA***NN^^'); // may print AA478NNhU
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 7 years ago.
Improve this question
I am just trying to figure out how I can get the actual digits of a figure that has been calculated within php and formatted to have 2 decimal places.
so say its calculated it to be 45.76 I am trying to figure out how I can get the 76 from it for an if statement. Basically I want it to look and just say that if it's 00 then remove them, if not, show them.
Thanks
Try :
function showDecimals($v){
$n = abs($v);
$whole = floor($n);
$fraction = $n - $whole;
return $fraction > 0
}
And...
if (showDecimals(10.15)){
//Show
}else{
//Remove?
}
You want to show a whole number if there is no decimal place, and 2 decimals of precision if not?
Method 1
function formatNumber($n) {
$n = round($n*100)/100;
return ''+$n;
}
This simply rounds it to 2 decimals of precision. Zero truncation is automatic.
Usage
echo formatNumber(0); //0
echo formatNumber(0.5); //0.5
echo formatNumber(0.894); //0.89
echo formatNumber(0.896); //0.9
echo formatNumber(1.896); //1.9
Method 2
Or if you 1.9 to display as 1.90, I suppose this would work:
function formatNumber($n) {
if ($n == 0)
return ''+$n;
$str = ''.round($n*100)/100;
$dotpos = strrpos('.', $str);
if (strlen(substr($str, $dotpos+1)) === 2)
$str .= '0';
return $str;
}
Usage:
echo formatNumber(0); //0
echo formatNumber(0.5); //0.50
echo formatNumber(0.894); //0.89
echo formatNumber(0.896); //0.90
echo formatNumber(1.896); //1.90
Edit: Accidentally posted broken version of method 2, should be fixed now.
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 have generate a 16 digit number and i want to extract one number from every 4 digits of that 16 digit number. For e.g: 1234567892345678. I want to extract 2 from 1234, 7 from 5678, 3 from 9034 & 7 from 5678. Then store it in another variable $a. the extraction will be in a random manner.
You can try this -
$d = '1234567892345678';
$s = str_split($d, 4); // split in 4 digits
$n = array_map(function($x) {
return substr($x, rand(0, 3), rand(1, 1)); // extract single digit random number
}, $s);
$n will hold the random numbers.
I might be late at answering this question but you can simply use strlen function along with for loop like as
$str = "1234567892345678";
for($i = 0; $i < strlen($str);$i += 4){
echo $str[$i+rand(0,3)];
}
Here you have a one-liner:
$s = $string[rand(0,3)].$string[rand(4,7)].$string[rand(8,11)].$string[rand(12,15)];
echo $s;
Another one-liner:
for($s='',$i=0;$i<10; $s.=$string[rand($i,$i+=3)]);
echo $s;