I want to replace 1st and last digit of a number series with *. such as I have
$number = 123435987345735372;
I want to get such as
*535738753957353*
like this?
$number = 123435987345735372;
$new_number = substr($number, 1, -1);
$new_number = "*".$new_number."*";
echo"original number: $number<BR>new number: $new_number";
Like what Norbert van Nobelen said, check out the manual at php.net
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'm trying to replace the first 4 digits of a number with X, for example:
$num= 1234567890
I want the output to appear like this: XXXX567890
I have tried the function:
$new = substr($num, 0, -4) . 'xxx';
but It only removes the last 4 digits so what should I do ?
You can use the same in opposite
$num= 1234567890;
$new = 'xxxx' . substr($num, 4);
echo $new;
second parameter tells about starting point for string and parity(positive or negative) tells about direction. positive number means to right of string and negative number means to left of string.
http://php.net/manual/en/function.substr.php
With substr_replace function:
$num = 1234567890;
print_r(substr_replace($num, 'XXXX', 0, 4)); // XXXX567890
Another solution is to use str_pad which "fills up" the string to 10 elements with "X".
$num= 1234567890;
Echo str_pad(substr($num,4), 10, "X",STR_PAD_LEFT);
https://3v4l.org/tKtB7
Or if the string lenght is not always 10 use:
Echo str_pad(substr($num,4), strlen($num), "X",STR_PAD_LEFT);
I think this one can be helpful for achieving desired output.
Solution 1:
Try this code snippet here
<?php
ini_set('display_errors', 1);
$num= 1234567890;
echo "XXXX".substr($num, 4);//concatenating 4 X and with the substring
Solution 2: Try this code snippet here
<?php
ini_set('display_errors', 1);
$num= 1234567890;
$totalDigits=4;
echo str_repeat("X", $totalDigits).substr($num, $totalDigits);// here we are using str_repeat to repeat a substring no. of times
Output: XXXX567890
If have written a tiny function to do tasks like this.
function hide_details($str, $num = 4, $replace = 'x') {
return str_repeat($replace, $num).substr($str, $num);
}
echo hide_details('1234567890');
How to validate NHS number using check digit 11 (mod 11) using PHP.
After a long search on the internet trying to find a function that I can use to validate check digit 11 (mod 11) number, I couldn't find so I end up developing one myself and share it here. I hope someone my find it useful.
The code with comments:
###### Check Digit 11 Modulus
function mod11($nhs_no){
//Get Last Number
$checkDigit = (int)substr($nhs_no, -1);
//Get Remaining Numbers
$numbers = substr($nhs_no, 0, -1);
// Sort Numbers Into an Array
$numbers_array = str_split($numbers);
//Define the base for the weights
$base=2;
// Multiply the weights to the numbers
$numbers_array_reversed = array_reverse($numbers_array);
foreach($numbers_array_reversed as $number){$multiplier[$number.'x'.$base]=(float)$number * $base;$base++;}
//Get the total sum
$sum =(float) array_sum($multiplier);
$modulus = 11;
// Divide the sum by check digit 11
$divider = (float)($sum / $modulus);
// Get the remainder
$remainder = (int)(($divider - (int)$divider)*$modulus);
// Check if the remainder is matching with the last check digit
$results = (int) ($modulus-$remainder);
// Return true or false
return ($results == $checkDigit ? true : false);
}
How can I get the first and the last digit of a number? For example 2468, I want to get the number 28. I am able to get the ones in the middle (46) but I can't do the same for the first and last digit.
For the digits in the middle I can do it
$substrmid = substr ($sum,1,-1); //my $sum is 2468
echo $substrmid;
Thank you in advance.
You can get first and last character from string as below:-
$sum = (string)2468; // type casting int to string
echo $sum[0]; // 2
echo $sum[strlen($sum)-1]; // 8
OR
$arr = str_split(2468); // convert string to an array
echo reset($arr); // 2
echo end($arr); // 8
Best way is to use substr described by Mark Baker in his comment,
$sum = 2468; // No need of type casting
echo substr($sum, 0, 1); // 2
echo substr($sum, -1); // 8
You can use substr like this:
<?php
$a = 2468;
echo substr($a, 0, 1).substr($a,-1);
You can also use something like this (without casting).
$num = 2468;
$lastDigit = abs($num % 10); // 8
However, this solution doesn't work for decimal numbers, but if you know that you'll be working with nothing else than integers, it'll work.
The abs bit is there to cover the case of negative integers.
$num = (string)123;
$first = reset($num);
$last = end($num);
I need to split a string in two so I can do a SELECT query in my DB. For example, I have the number '0801'. I need to split that number into '08' and '01'. How do I manage to do that?
This is easy to do using the substr() function, which lets you pull out parts of a string. Given your comment that you always have a four-digit number and want it split into two two-digit numbers, you want this:
$input = "0801";
$left = substr($input, 0, 2);
$right = substr($input, 2);
echo "Left is $left and right is $right\n";
According to your comment
The first 2 numbers. It's allways a 4 digit number and I allways need
to split it in 2 two digit numbers
You can simply use
$value = '0801';
$split = str_split($value, 2);
var_dump($split[0]);
var_dump($split[1]);
Just keep in mind $value variable should always be of a string type, not int.
you can use str_split and list
$str = '0801';
list($first,$second) = str_split($str,2);
echo $first;
// 08
echo $second;
// 01
No one gave a MySQL answer yet...
If you're selecting that number..
SELECT
SUBSTR(colname, 0, 2) as firstpart,
SUBSTR(colname, 2, 2) as secondpart
FROM table