PHP / MySQL Multiple substrings? - php

I have a field in my table that contains numbers for instance: 7121968 - what I would like to do is add a . before the last four and before the last 6 digits so that it looks like: 7.12.1968.
I can use substring to just show the last 4 digits etc - is what i am trying to do possible using substring and if so could someeone point me in the right direction? thanks.

$x = '7121968';
$x2 = substr($x,0,-6) . '.' . substr($x,-6,2) . '.' . substr($x,-4);

Do like this:
SELECT CONCAT(SUBSTRING(<column>, 1, 1), '.', SUBSTRING(<column>, 2, 2), '.', SUBSTRING(<column>, 4)) FROM <table>

Related

php - Masking a Phone Number regardless of length

I've tried the following functions
str_repeat("*", strlen($value['number'])-2) . substr($value['number'], -2)
and str_pad(substr($value['number'], -2), strlen($value['number']), '*', STR_PAD_LEFT)
However they both yield the same result: ******43
What I am tying to achieve is this: 70****43, but dynamically regardless of number length.
Any suggestions how it can be done?
See below:
$num = '70564843';
echo substr( $num, 0, 2 ) // Get the first two digits
.str_repeat( '*', ( strlen( $num ) - 4 ) ) // Apply enough asterisks to cover the middle numbers
.substr( $num, -2 ); // Get the last two digits
Output:
70****43
It's kind of dynamic:
$phoneNumber = 123456789; //your phone number
$showFirstDigits = 2; //how many digits to show in the beggining of the phone number
$showLastDigits = 2; // how many digits to show in the end of the phone number
echo(
substr_replace($phoneNumber,str_repeat('*',strlen($phoneNumber) - $showFirstDigits - $showLastDigits),
$showFirstDigits,
strlen($phoneNumber) - $showFirstDigits - $showLastDigits));

Impossible to round or truncate decimal Laravel

I have a problem in my Controller in Laravel 4. I try to round (or truncate) a decimal with 2 digits after comma, but it doesn't work.
I tried something like this :
round(888/100, 2);
Or :
floor(888*100)/100;
But I have 8.880000000000001 instead of 8.88.
Why?
(In this example I take this number but it can be an other number)
Try this after rounding :
number_format((float)$number, 2, '.', '');
try this
number_format($number, 2, '.', ',');
// 2 = decimal places | '.' = decimal seperator | ',' = thousand seperator

Splitting strings through php to query mysql

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

PHP ordered Unique ID

Hello I hope someone could help me cus Iam little bit confused about task I have to do in PHP
I need php file that is unique registration ID with these parameters:
First is AA00001 and next one is DF00002.
So first letter + 3 and second + 5, but numbers going in +1 order.
Could someone give me hint how to achieve this?
Thank you!
In pseudocode:
get previous ID
separate first letter, second letter and number
convert first letter to number, add 3, modulo 26, convert back to letter
convert second letter to number, add 5, modulo 26, convert back to letter
add 1 to number, add zero-padding to reach 5 digits
concatenate them all together
set this as the new "previous ID"
Note that you'll need to ensure that this happens atomically - i.e. that you don't have multiple processes working on the same ID, else they'd get the same "next" ID. This will IMHO be the hardest part.
You can use substr to split the ID, dechex and hexdec to convert to/from decimal to hexadecimal, which gives you the A+3=D part, and you can use str_pad to front pad the integer with zeros, which gives you the second part, and then you just concatenate them.
ETA: Something like this:
$id = 'AA00001';
$first = dechex((hexdec(substr($id,0,1))+3)%16);
$secnd = dechex((hexdec(substr($id,1,1))+5)%16);
$int = str_pad(substr($id,2)+1,5,"0",STR_PAD_LEFT);
$newid = strtoupper($first.$secnd.$int);
ETA2: Unless you meant to go AA00001, DF00002, GK00003, JP00004, MU00005, PZ00006, SE00007 etc in which case you need
$first = chr(((ord(substr($id,0,1))-62)%26)+65);
$secnd = chr(((ord(substr($id,1,1))-60)%26)+65);
$lastid = 'AA00001';
$first = substr($lastid, 0, 1);
$second = substr($lastid, 1, 1);
$numeric = substr($lastid, 2);
$next_first = chr(((ord($first) - ord('A') + 3) % 26) + ord('A'));
$next_second = chr(((ord($second) - ord('A') + 5) % 26) + ord('A'));
$next_numeric = sprintf('%05d', intval($numeric) + 1);
$new_id = $next_first . $next_second . $next_numeric;
// DF00002
First you have to parse the last reg id.(using substr) Then,
store each value in a var corresponding to the place
$first , $second, $numberpart. then
$first = ($first + 3 ) % 16;
$second =($first + 5 ) % 16;
$number = $number + 1;
THen update the record accordingly converting$first, $second to their appro. letters.

Add a period to the middle string PHP

It will actually be a decimal but that is not the main point. I will have a set of numbers like:
8976
8765
3454
3453
10198
What I am wanting to do is add a decimal 2 places from the right. So the first would be 89.76 and so forth.
Can't you just multiply each by 0.01?
$formatted = number_format($unformatted_number / 100, 2, '.', '');
2 - decimal places
'.' - decimal separator
'' - thousands separator
docs for the function are here.
try this
$number = 8976;
$number = (float)$number/100;
results:
89.76
You may have to do some checking to see how many digits the number is, i.e 89768 would be devided by 1000 and so on.
Comments are available,
//the string you need to split
$string = "123456";
// read from right 2 character
$rightNums = substr($string, -2, 2);
// maximum 100 character to the left defined now
$otherNums = substr($string, -4, 100);
// pront them just with . between
echo $otherNums.".".$rightNums; ?>
hope it help much.
Try with this
$tmpString = substr("8976", 0, -2);
$finalString = str_replace($tmpString, "." . $tmpString, "8976");
echo $finalString;

Categories