I have a feed in array form which contains area of construction
$x['AC']="25";
sometime it comes with the measurement value attached in the string itself
$x['AC']="25mt2"; or $x['AC']="25 mt2"; or $x['AC']="25m2";
$x['AC']="25ht2"; or $x['AC']="25 ht2"; or $x['AC']="25h2";
how can I detect this and then clean the value of the array to just number .
and once its detected I have to create a string like
mt2[;;;]25 or ht2[;;;]25
Thanks in advance
I would use a preg to remove other caracters than numbers:
$x['AC'] = preg_replace('/\D+/','',$x['AC']);
But would leave any additional value besides the 25 that you refer, but if 25 is the first number, i would use a preg to get the first numbers in the string:
if(!is_numeric($x['AC'])){
preg_match('/^\d+/',$x['AC'],$nr);
$x['AC'] = $nr[0];
}
To get the inside mt or ht also, i would modify the regex like:
if(!is_numeric($x['AC'])){
preg_match('/^(\d+)\s?(.*)$/',$x['AC'],$nr);
$x['AC'] = $nr[1];
$mtORht = $nr[2];
}
OK, resuming, so m2 is default, and for anything else you wish to create a variable with that value in array:
OK, float values too:
if(!is_numeric($x['AC'])){
preg_match('/^([0-9\.]+)\s?(.*)$/',$x['AC'],$nr); //$x['AC']="25 ht2"
$theValue = $nr[1]; //25
$theMeasurementName = $nr[2]; //ht2
//creating the variable:
if(!stristr($theMeasurementName, 'm')){ // if no "m" is found, the m you wish to omit.
${$theMeasurementName} = $theValue; // $ht2[] = 25
}
}
You can just cast it to an integer and if there is a non numerical value it will get cut off, like this:
$x['AC'] = (int) $x['AC'];
example input/output:
25 -> 25
25mt2 -> 25
25ht2 -> 25
25 mt2 -> 25
25 ht2 -> 25
25m2 -> 25
25h2 -> 25
EDIT:
As per your updated question you can use preg_match() to create your string:
preg_match("/^(\d+)\s*(.*?)$/", $x["AC"], $m);
echo (empty($m[2])?"m2":$m[2]) . "[;;;;]" . $m[1];
$match = array();
preg_match('/^\d+/', $string, $match));
echo $match[0];
Regex:
^ -> begin with
\d+ -> any digit repeated
Related
I have this number 12455, I want to extract the last two digits like $result = 55 .
I tried :
$order_id = 12455;
$lastTwoNumbers = $order_id[strlen($order_id)-2];
you can use the following code:
$lastTwoNumbers = substr($order_id, -2);
You can use substr() to get the last two chars of that string (use a negative "start" parameter to count from the end):
<?php
$order_id = 12455;
$lastTwoNumbers = (int) substr($order_id, -2);
echo $lastTwoNumbers;
Will return 55. (The result will be of type string, (int) makes sure it will be integer after)
I want to display a large number with a leading zero and a dot after.
The balance i want to display starts with 0.000000000000000000 ( 18 zeros after the dot ). This should be able to go up to 99.00000000000000000.( 17 zeros after the dot ).
I did a lot of trial and error but i just can't seem to get the dot in there. As far as for the zeros i got it working. What i have now is:
$leadingBalance = sprintf("%019d", $balance);
echo $leadingBalance;
This will display the correct balance but i need to place the dot in there. It means that if my balance has 17 or 18 numbers it should place the dot as 0.0000... If the balance has 19 numbers it should place the dot as 00.0000...
Whatever i try, how much i look up i can't figure it out.
For eg:
$n1 = 0;
$n2 = 99;
echo number_format($n1,18)."<br>";
echo number_format($n2,18)."<br>";
See the documentation for number_format: http://php.net/number_format
The functions parameters are:
string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )
So use:
number_format(1000000000000000, 2, '.', '');
Which means that you don't use any (= empty string) thousands separator, only a decimal point.
or if you just want padding of 19 zero after decimal point
just use
sprintf("%0.19f",$number);
or else
if u want a number always 20 digit without caring about whole no and decimal value than use str_pad()
eg:
$no = sprintf("%0.2f",100); //100.00
this will convert your no to decimal point with 2 digit after decimal now just pad some digit if require to make it 20 digit long
echo str_pad($no,20,"0"); //100.00(15 zero after this)
this will check no of digit available and pad 0 to make it 20 digit
for more ref:https://www.w3schools.com/php/func_string_number_format.asp
You are using %019d when you actually wants a float number, try this:
<?php
$format = '%0.19f';
$args = 9;
$result = sprintf ($format, $args);
//$result will be equal to 9.0000000000000000000
?>
<?php
$format = '%0.19f';
$args = 99;
$result = sprintf ($format, $args);
//$result will be equal to 99.0000000000000000000
?>
Edit:
Since it looks like you want your whole number to be equal to 20 digits only, you may try to do some math if number_format doesn't do your job. You can try something like:
<?php
$number = 999;
$number_length = strlen($number);
$format_len = 20 - $number_length;
$format = '%0.'. $format_len .'f';
$result = sprintf($format, $number);
?>
I want to format the credit cards like below when i display it,
Eg:
1234 4567 9874 1222
as
1xxx xxxx xxx 1222
Is there any formatting function like this in Yii ?
No - but there's nothing wrong with using straight PHP.
If you always want the 1st and the last 4 chars you can do something like this:
$last4 = substr($cardNum, -4);
$first = substr($cardNum, 0, 1);
$output = $first.'xxx xxxx xxxx '.$last4;
There are many ways to do this, nothing Yii specific
You could do it using str_split (untested):
$string = "1234 4567 1234 456";
$character_array = str_split($string);
for ($i = 1; $i < count($character_array) - 4; $i++) {
if ($character_array[$i] != " "){
$character_array[$i] = "x";
}
}
echo implode($character_array);
So we are creating an array of characters from the string called
$character_array.
We are then looping thru the characters (starting from position 1,
not 0, so the first character is visible).
We loop until the number of entries in the array minus 4 (so the last
4 characters are not replaced) We replace each character in the loop
with an 'x' (if it's not equal to a space)
We the implode the array back into a string
And you could also use preg_replace :
$card='1234 4567 9874 1222';
$xcard = preg_replace('/^([0-9])([- 0-9]+)([0-9]{4})$/', '${1}xxx xxxx xxxx ${3}', $card);
This regex will also take care of hyphens.
There is no in-built function in Yii.
What would be an elegant way of doing this?
I have this -> "MC0001" This is the input. It always begins with "MC"
The output I'd be aiming with this input is "MC0002".
So I've created a function that's supposed to return "1" after removing "MC000". I'm going to convert this into an integer later on so I could generate "MC0002" which could go up to "MC9999". To do that, I figured I'd need to loop through the string and count the zeros and so on but I think I'd be making a mess that way.
Anybody has a better idea?
This should do the trick:
<?php
$string = 'MC0001';
// extract the part succeeding 'MC':
$number_part = substr($string, 2);
// count the digits for later:
$number_digits = strlen($number_part);
// turn it into a number:
$number = (int) $number_part;
// make the next sequence:
$next = 'MC' . str_pad($number + 1, $number_digits, '0', STR_PAD_LEFT);
using filter_var might be the best solution.
echo filter_var("MC0001", FILTER_SANITIZE_NUMBER_INT)."\n";
echo filter_var("MC9999", FILTER_SANITIZE_NUMBER_INT);
will give you
0001
9999
These can be cast to int or just used as they are, as PHP will auto-convert anyway if you use them as numbers.
just use ltrim to remove any leading chars: http://php.net/manual/en/function.trim.php
$str = ltrim($str, 'MC0');
$num = intval($str);
<php
// original number to integer
sscanf( $your_string, 'MC%d', $your_number );
// pad increment to string later on
sprintf( 'MC%04u', $your_number + 1 );
Not sure if there is a better way of parsing a string as an integer when there are leading zero's.
I'd suggest doing the following:
1. Loop through the string ( beginning at location 2 since you don't need the MC part )
2. If you find a number thats bigger than 0, stop, get the substring using your current location and the length of the string minus your current location. Cast to integer, return value.
You can remove the "MC" par by doing a substring operating on the string.
$a = "MC0001";
$a = substr($a, 2); //Lengths of "MC"
$number = intval($a); //1
return intval(str_replace($input, 'MC', ''), 10);
How to make a number 8 digit as standard digit. The number will be get to a user id from database.
Example
user_id = 1 // This should should be echo as 00000001
user_id = 11 // This should should be echo as 00000011
user_id = 111 // This should should be echo as 00000111
How can I code this? Please help thanks.
You can use printf function with %08s as the format string:
printf("%08s",$user_id);
If you want to store the result back in the string you can use sprintf as:
$user_id = sprintf("%08s",$user_id);
The format specifier %08s says:
s : Interpret the argument as a string
8 : Print the string left justified within 8 alloted places
0 : Fill the unused places with 0
You could use printf:
printf("%08d", $user_id);
PHP has sprintf:
$user_str = sprintf("%08d", $user_id);
echo $user_str;
You can do with str_pad
echo str_pad($user_id,8,'0',STR_PAD_LEFT);
$user_id = str_pad($user_id, 8, "0", STR_PAD_LEFT);
function leadingZeroes($number, $paddingPlaces = 3) {
return sprintf('%0' . $paddingPlaces . 'd', $number);
}
Source.