My PHP file receives (via $_POST) strings (with constant prefix) like:
(constant prefix in this example = 'astring'; figure before and after the decimal point can vary in size)
astring1.1
astring1.2
..
astring23.2
astring23.6
How to get the value behind the decimal point? I know how to extract the total figure from the constant prefix, but I need to use the figures before and after the decimal point and don't know how to extract those. Using preg_match in some way?
If you want the number before the decimal. Try This.
$number = 2.33;
echo floor($number);
Try with explode like
$str = 'astring23.2';
$str_arr = explode('.',$str);
echo $str_arr[0]; // Before the Decimal point
echo $str_arr[1]; // After the Decimal point
list($before, $after) = explode(".", $string);
echo "$before is the value before the decimal point!";
echo "$after is the value after the decimal point!";
A simple way (php 5.3+):
$str = 'astring23.2';
$pre = strstr($str, '.', true);
Do you want something similar?
<?php
$string = "astring23.6";
$data = explode("astring", $string); // removed prefix string "astring" and get the decimal value
list($BeforeDot, $afterDot)=explode(".", $data[1]); //split the decimal value
echo "BeforeDot:".$BeforeDot." afterDot: ". $afterDot;
?>
If you want only numbers, use like this
$str = 'astring23.2';
$str_arr = explode('.',$str);
echo preg_replace("/[a-z]/i","",$str_arr[0]); // Before the Decimal point
echo $str_arr[1]; // After decimal point
Related
i have input form currency with format like this 1,000.00
and i want to save in database in decimal format like 1000.00
how to change it in controller, when save it it will automatically change to decimal format?
$num = '3,500.20';
$formattedNum = number_format($num, 3, '', '');
echo $formattedNum;
the code above convert to 3000 not 3500.20
you can use str_replace of php to get you result:
<?php
$num = '3,500.20';
$formattedNum = str_replace(',', '', $num);
echo $formattedNum;
?>
str_replace funcation replace your ',' with '' a blank value,
To read more about str_replace you can read here
You can just remove comma with str_replace(',', '', $num) for example.
But better to get correct value right from your input.
Input format shouldn't affect actual input value which is 3500.20, that doesn't look as good practice.
Also number_format is not for converting string to numbers, on the contrary it makes pretty string from number(integer/float).
You can try str_replace for the desired result:-
$number = '3,500.20';
$number = str_replace(',', '', $number);
echo $number;
Output
3500.20
I was wondering if there's a combo of functions or a direct function that can count how many numbers appears in a string, without use a long-way as str_split and check every character in a loop.
From a string like:
fdsji2092mds1039m
It returns that there's 8 numbers inside.
You can use filter_var() with the FILTER_SANITIZE_NUMBER_INT constant, then check the length of the new string. The new string will contain only numbers from that string, and all other characters are filtered away.
$string = "j3987snmj3j";
$numbers = filter_var($string , FILTER_SANITIZE_NUMBER_INT);
$length = strlen($numbers); // 5
echo "There are ".$length." numbers in that string";
Note that each number will be counted individually, so 137 would return 3, as would 1m3j7.
Live demo
Other solution:
function countNumbers(string $string) {
return preg_match_all('/\d/', $string, $m);
}
You can use regular expression
Try like this:
$myString = 'Som3 Charak1ers ar3 N0mberZ h3re ;)';
$countNumbers = strlen((string)filter_var($myString, FILTER_SANITIZE_NUMBER_INT));
echo 'Your input haz ' . $countNumbers . ' digits in it, man';
You can also make a function out of this to return only the number, if you need it.
Following code does what you intend to do:
<?php
$string = 'dsfds98fsdfsdf8sdf908f9dsf809fsd809f8s15d0d';
$splits=str_split($string);
$count=0;
foreach ($splits as $split){
if(is_numeric($split)){
$count++;
}
}
print_r($count);
Output: 17
I have a string with numbers, stored in $numbers:
3,6,86,34,43,52
What's the easiest way to get the last value after the last comma? In this case the number 52 would be the last value, which I would like to store in a variable.
The number can vary in size, so trying:
substr($numbers, -X)
does not help me out I think.
This should work for you:
Just use strrpos() to get the position of the last comma and then use substr() to get the string after the last comma, e.g.
$str = "3,6,86,34,43,52";
echo substr($str, strrpos($str, ",") + 1);
output:
52
Just explode the string by the separator character and pick the last of the resulting tokens:
<?php
$string = '3,6,86,34,43,52';
$tokens = explode(',', $string);
echo end($tokens);
An alternative would be to use a regular expression:
<?php
$string = '3,6,86,34,43,52';
preg_match('/,([0-9]+)$/', $string, $tokens);
echo end($tokens);
Personally I have the opinion that efficiency is less important that easy of reading and understanding the code these days. Computation power is cheap, developers are expensive. That is why I would use the first approach, expect when the number of elements in the string gets big.
You can do it like this:
$numbers = "3,6,86,34,43,52";
$arr = explode(",",$numbers);
echo $arr[count($arr)-1];
I'd just explode it to an array, and get the last element:
$numbers = '3,6,86,34,43,52';
$arr = explode(',', $numbers);
echo $arr[count($arr) - 1];
A direct, single-function approach would be to trim every upto the last comma.
Code: (Demo)
$numbers = "3,6,86,34,43,52";
echo preg_replace('/.*,/', '', $numbers);
// 52
I have a string, "Chicago-Illinos1" and I want to add one to the end of it, so it would be "Chicago-Illinos2".
Note: it could also be Chicago-Illinos10 and I want it to go to Chicago-Illinos11 so I can't do substr.
Any suggested solutions?
Complex solutions for a really simple problem...
$str = 'Chicago-Illinos1';
echo $str++; //Chicago-Illinos2
If the string ends with a number, it will increment the number (eg: 'abc123'++ = 'abc124').
If the string ends with a letter, the letter will be incremeted (eg: '123abc'++ = '123abd')
Try this
preg_match("/(.*?)(\d+)$/","Chicago-Illinos1",$matches);
$newstring = $matches[1].($matches[2]+1);
(can't try it now but it should work)
$string = 'Chicago-Illinois1';
preg_match('/^([^\d]+)([\d]*?)$/', $string, $match);
$string = $match[1];
$number = $match[2] + 1;
$string .= $number;
Tested, works.
explode could do the job aswell
<?php
$str="Chicago-Illinos1"; //our original string
$temp=explode("Chicago-Illinos",$str); //making an array of it
$str="Chicago-Illinos".($temp[1]+1); //the text and the number+1
?>
I would use a regular expression to get the number at the end of a string (for Java it would be [0-9]+$), increase it (int number = Integer.parse(yourNumberAsString) + 1), and concatenate with Chicago-Illinos (the rest not matched by the regular expression used for finding the number).
You can use preg_match to accomplish this:
$name = 'Chicago-Illinos10';
preg_match('/(.*?)(\d+)$/', $name, $match);
$base = $match[1];
$num = $match[2]+1;
print $base.$num;
The following will output:
Chicago-Illinos11
However, if it's possible, I'd suggest placing another delimiting character between the text and number. For example, if you placed a pipe, you could simply do an explode and grab the second part of the array. It would be much simpler.
$name = 'Chicago-Illinos|1';
$parts = explode('|', $name);
print $parts[0].($parts[1]+1);
If string length is a concern (thus the misspelling of Illinois), you could switch to the state abbreviations. (i.e. Chicago-IL|1)
$str = 'Chicago-Illinos1';
echo ++$str;
http://php.net/manual/en/language.operators.increment.php
I need to transform a String to a Float.
I will receive Strings like this:
$string = "1.70 m";
$string2 = "2.445 m";
How can I easly transform this strings to:
$float1 = 1.70;
$float2 = 2.445;
Can someone give me some clues?
Best Regards,
Those are floats, not integers. Integers do not have decimal points.
To answer your question, you can simply typecast the strings directly, the conversion will strip off the units as those aren't numeric characters:
$string = "1.70 m";
$float = (float) $string;
you can get it by
echo (float)array_shift(implode(' ', $string));
Update :
echo (float) $string;
The easiest way to do this is probably with the floatval() function:
http://ca.php.net/manual/en/function.floatval.php
For exmaple:
floatval("1.70 m");
gives you:
1.7
$integer = intval($string);
Enjoy :D