I have an input that looks like this
<input value="{{gameAccount.getAccountNumber() }}" disabled name="accountNumber" type="text">
which displays:
123456789
but I want it to display
*****6789
I get confused when it is in input value in this particular case with gameAccount.getAccountNumber()
substr($something?, 0, -4) . '****';
How do I go about this? thanks in advance
I also saw substr_replace() function...
As #ficuscr points out this is a better one liner for this:
$gameId = '123456789';
$gameId = str_repeat('*', strlen($gameId) - 4) . substr($gameId, -4);
There may be a more elegant way to do this but this will work:
$gameId = '123456789';
$gameIdLenToMask = strlen($gameId) - 4;
$mask = str_pad('', $gameIdLenToMask, '*');
$gameIdMasked = substr_replace($gameId, $mask, 0, $gameIdLenToMask);
// Prints: "*****6789"
var_dump($gameIdMasked);
For more information see the docs for str_replace and str_pad on php.net.
One other option, you can use the length minus four as the limit argument for preg_replace.
$new = preg_replace('/./', '*', $str, strlen($str) - 4);
If you aren't sure that your string will be at least four characters, you can use max to be sure the limit doesn't go negative.
$new = preg_replace('/./', '*', $str, max(0, strlen($str) - 4));
Related
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');
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
I'm trying to replace some characters from a string using str_pad and I can't get it to work at all and I have no idea why it doesn't work.
Code:
<?php
$t = "abcdefghij";
$t = str_pad($t, 4, "0");
echo $t;
?>
Expected:
abcd000000
Result:
abcdefghij
I also tried:
$t = sprintf("%04x", $t);
Which results in:
0000
If you have a variable length string, you can use the below
$str = "abcdefghijzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz";
$head = substr($str, 0, 4); // construct the first part of your string
$tail = substr($str, 4); // get the second part of the string
print $head . str_repeat('0',strlen($tail));
// or all in one go
$number = 4;
print substr($str, 0, $number) . str_repeat('0',strlen(substr($str, $number)));
//will output
//abcd0000000000000000000000000000000000000
To replace everything except the first x number of characters in your string.
Because your string is longer than four characters so str_pad() will not append anything to it.
If the value of pad_length is negative, less than, or equal to the length of the input string, no padding takes place, and input will be returned.
If you want to always append four zeros just concatenate them onto your string:
<?php
$t = "abcdefghij";
$t = '0000' . $t;
echo $t;
?>
What you are looking for is a mixture of str_pad and substr, using the two like this:
echo str_pad(substr($str, 0, 4), strlen($str), '0', STR_PAD_RIGHT);
Will give you output like this:
abcd000000
You may however, need to tweak the numbers to get your desired results.
Here is a function you can use:
function pad($str, $length, $value= '0', $side = STR_PAD_RIGHT){
return str_pad(substr($str, 0, $length), strlen($str), $value, $side);
}
echo pad('abcdefghij', 4);
You need a different functions. str_pad() adds to the string and you are wanting to replace:
$t = substr_replace($t, str_repeat('0', strlen($t)-4), 4);
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);
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;