extracting numbers from in a string [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 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);

Related

Format timezone offsets that starts with + or - [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 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

Best approach to explode high values [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
What's best approach to explode and separate high numeric values so it will be displayed in more legible way?
For example
100000000 should be converted to 100 000 000, or 10000.00 to 10 000.00
Use the number_format function.
$number = 1234.56;
number_format($number, 2, ',', ' '); // 1 234,56
As it's tagged as PHP you are looking for function called
number_format()
More details how to use it in documentation http://php.net/manual/en/function.number-format.php
Try this:-
$value = 100000000;
echo number_format($value , 0, ' ', ' ');
Output:- 100 000 000
number_format() function will accept one, two, and four arguments. Not three. and it works as follows:
// formatting with ","
$number = 1234.56;
var_dump(number_format($number)); // 1,235
//formatting with decimals
$number = 1234.56;
var_dump(number_format($number, 2)); // 1,235.56
//formatting with thousands seperator
$number = 1234.56;
var_dump(number_format($number, 2, '.', '')); // 1235.56

How to split 4113.52318N in two parts 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 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

PHP Match string with numbers and get next number (as Year) [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 6 years ago.
Improve this question
These are my strings
2012-13
2013-14
2014-15
2015-16
2016-17
2017-18
I my string is 2014-15 as current financial year, I want my next my next financial year as 2015-16
How can I achieve this with PHP? I get these year list form mysql table, but I want next financial year in php
Try this:
$financial_year = "2012-13";
$fin_array = explode("-", $financial_year);
$next_fin_array[0] = $fin_array[0] + 1;
$next_fin_array[1] = $fin_array[1] + 1;
$next_financial_year = implode("-", $next_fin_array); // Gives 2013-14
Hope this helps.
$myStr = '2014-15';
$yr1 = (int) substr($myStr, 0, 4);
$yr2 = (int) substr($myStr, -2);
echo ($yr1+1).'-'.($yr2+1);
// 2015-16
$financeYear = DateTime::createFromFormat ( "Y-d" ,"2014-15");
$financeYear->modify('+ 1 year');
echo $financeYear->format("Y-d");
Result
2015-15

Extract one digit from every four digit in a sixteen digit integer and store in a variable 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 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;

Categories