replacing first few digits with # - php

I want to hide the first 6 digits of social security
I have them like 123-456-7890
I want to show ###-###-7890
how can I do that
thanks

$ssn = '123-456-7890';
$ssn = '###-###-'.substr ($ssn , 8);

You can use the substr method to get the rightmost 4 characters from the SSN using a negative length:
$ssn = '111-222-4567';
$ssn_obscured = '###-###-' . substr($ssn, -4);
See http://us.php.net/substr for more information on the method.

Related

How to delete the first digit number in PHP

I have a data of phone number starts with +63 and 09
for example the user insert 09123456789 then save. The user shouldn't success if he enters +639123456789 because it was same number at all.
I tried using substr_replace
$data3 = substr_replace("+63", "0",0, 3);
is there other option? i think the substr_replace will have error in the future.
thanks
If the +63 must be at the start, you may use a preg_replace like
$s = preg_replace('~^\+63~', '0', $s);
Here,
^ - start of a string position
\+ - a literal +
63 - a 63 substring.
See the regex demo and a PHP demo.
Please also consider Tpojka's suggestion to use a combination of intl-tel-input on front-end and libphonenumber-for-php on back-end if you need to sanitize and validate international phone numbers.
In this case you can have a condition to check and have str_replace
if there is a + on your request then make remove the first 3 letters including +
if (substr($phone, 0, 1) == '+') {
$phone = str_replace(substr($phone, 0, 3), '0', $phone);
}
$phone_number = $phone;

PHP strrchr parse int

I've got a problem. This is my PHP code :
$extract = $query;
$extractpoint = strrchr($extract, ".");
So, $extract is a parse_url of my website address.
Exemple : http://test.com?param.6
$extract = param.6 and $extractpoint = .6
BUT, I want a solution to have only the 6, without the point.
Can you help me with that ?
The easiest solution would be restructuring the URL. I that is not possible though you can use strpos to find the position of your specific character and then use substr to select the characters after it.
$extract = 'param.6';
echo substr($extract, strpos($extract, '.') + 1);
Demo: https://3v4l.org/CudTAG
(The +1 is because it returns the position of the match and you want to be one place past that)
There are different ways:
Filter only numbers:
$int = filter_var($extractpoint, FILTER_SANITIZE_NUMBER_INT);
Replace the point
$int = str_replace('.', '', $extractpoint)
//$int = str_replace('param.', '', $extractpoint)
Use regex
/[0-9+]/'
strrchr() results the count of the last instance of a character in a string. In order to get the next character add 1 to the count. Then use substr() to extract the next character from the string.
http://php.net/manual/en/function.strrchr.php
http://php.net/manual/en/function.substr.php

Big numbers regex

$value = preg_replace("/[^0-9]+/", '', $value);
How could I edit this regex to get rid of everything after the decimal point? There may or may not be a decimal point.
Currently "100.1" becomes 1001 but it should be 100.
Complete function:
function intfix($value)
{
$value = preg_replace("/[^0-9]+/", '', $value);
$value = trim($value);
return $value + 0;
}
It is used to format user input for numbers as well as servers output to format numbers for the DB. The functions deals with very large numbers, so I can't use intval or similar. Any extra comments to improve this function are welcome.
You could just change the regex to /[^0-9].*/s.
.* matches zero or more characters, so the first character that is not a digit, and the digits that immediately follow, would be deleted.
You need to have a pattern that starts the search with a decimal place. At the moment you're only deleting the . not the numbers after it... So you could do '/\.[\d]+/'
$text = "1201.21 12 .12 12.21";
$text = preg_replace('/\.[\d]+/', '' ,$text);
The above code would result in $text = "1201 12 12"
Why not $value = round($value, 0);? This can handle large values and is meant to get rid of the following decimals mathematically (I'd rather work with numbers as numbers not as strings). You can pass PHP_ROUND_HALF_DOWN as a third parameter if you want to just get rid of the decimals 10.7 -> 10. Or floor($value); could work too.

Replace Leading zero with '+' in PHP

I want to replace the leading zero in a phone number with '+' and country code.
If the phone number starts with zero (ex: 07512345678) then I want to remove the leading zero and replace with '+' and country code else (ex: 7512345678)just add '+' and country code.
What would the way to do that in PHP?
Use preg_replace()
$newNumber = preg_replace('/^0?/', '+'.$countryCode, $phoneNumber);
The first parameter is the regular expression, which is looking for that leading zero of yours. The second is what you want to replace it with (the plus sign concatenated with the country code.). Finally, $phoneNumber is the original phone number.
The replaced value is assigned to the variable $newNumber. Feel free to change the variables to fit your code.
Use substr_replace(), no need for regex or if blocks.
$number = '07512345678';
$country_code = '44';
$new_number = substr_replace($number, '+'.$country_code, 0, ($number[0] == '0'));
You could use preg_replace:
$newNumber = preg_replace("/^0/", "+", 07512345678)
This will replace the first character of each number if and only if it is zero. The regular expression used is: /^0/. The ^ tells it to look at the first character, and then only match a 0 thereafter. This 0 will be replaced with the second argument, the "+". The last argument is the source string.
Reference
Take a look at preg_replace here
Basic syntax for beginning REGEX here
You can use string manipulation:
$x = '012345678';
if ($x[0]=='0') $x[0] = ''; // delete leading zero
$x = '+1'.$x;
preg_match() is less effective I suspect, due to the complexity of the whole system.
<?php
$countryCode = "XX";
$phone = array("0123455", "7837373");
foreach( $phone AS $number ) {
if( $number[0] == '0' ) {
$number = "+{$countryCode}" . substr($number,1);
}
echo "{$number}\n";
}
PS: this is elementary. You should really got through some tutorials.

PHP and Money, converting money to cents

So I have done a fair bit of research on how to store "money" in a database and I think the system I want to use is
Converting Money into CENTS and then storing the CENTS in a MySQL DB with a field type of DECIMAL (19,4).
My question is, IF I have an input field from the user... how do I deal with multiple input types.
IE:
$input = "1,346.54"
$input = "10,985.23"
$input = "110,400.50"
$input = "1033.44"
etc etc...
What would be the best method for converting this to CENTS? As we have to deal with 'strings' and convert them to INT, and divide by 100... Everything that I try throws issues because of the "comma" separation with the numbers.
Any thoughts or direction would be greatly appreciated.
function getMoneyAsCents($value)
{
// strip out commas
$value = preg_replace("/\,/i","",$value);
// strip out all but numbers, dash, and dot
$value = preg_replace("/([^0-9\.\-])/i","",$value);
// make sure we are dealing with a proper number now, no +.4393 or 3...304 or 76.5895,94
if (!is_numeric($value))
{
return 0.00;
}
// convert to a float explicitly
$value = (float)$value;
return round($value,2)*100;
}
Looks like there is a NumberFormatter class which provides a parseCurrency method. Have a look at http://www.php.net/manual/en/numberformatter.parsecurrency.php
The example provided is
$fmt = new NumberFormatter( 'de_DE', NumberFormatter::CURRENCY );
$num = "1.234.567,89 $";
echo "We have ".$fmt->parseCurrency($num, $curr)." in $curr\n";
You can remove the commas like this:
$input = str_replace( ',', '', $input);
At this point, you can convert to cents by converting to a float and multiplying by 100. However, this is probably unnecessary. You would potential encounter precision issues when performing math operations, but simply storing the values in the database can be done in the original form without alteration of the value (assuming your DB tables are properly structured):
$input = (float)str_replace( ',', '', $input);
function convert_to_cents($v)
{
$v = str_replace(',','',$v);
$p = explode('.',$v);
if(strlen($p[1])<2){ $p[1] = $p[1]*10;}
return ($p[0]*100)+$p[1];
}
This converts most of the decimal currencies to their subunits.
$1,234,567.89 = 123456789
£ 1,234,567.89 = 123456789
€1.234.567,89 = 123456789
12,34 EUR = 1234
12,34 € = 1234
12,30 € = 1230
1,2 = 102
function convertDecimalCurrencyToSubUnit($str)
{
if( preg_match('/^(.+)[^\d](\d|\d\d)[^\d]*$/', $str, $m) )
return intval(preg_replace('/[^\d]/', '', $m[1]) . ( (strlen($m[2])>1) ? $m[2] : ('0' . $m[2]) ));
return 0;
}
Probably just remove the ',' and the '.' from the string, the result is the amount in cents.
You will probably need to parse the string from the back using strrpos ... If you find a comma 2 spots from the end, then its prob safe to assume its foreign currency and those are the CENTS... Once you determine that, use a regex to strip the remaining commas (after you convert the "CENTS" comma to a decimal of course) ... Now you have a normal DEC number to play with.
Use this to find the last comma in your string ... strrpos
Use this to replace the commas preg_replace
Here is a helpful regex website .. regexlib
//Finding the last comma
$yourCommaPos = strrpos($myString, ',');
if ($yourCommaPos == strlen($myString) - 2) {
//Comma is foreign currency decimal
// Replace with '.'
} else {
//Not foreign Currency so...
//Strip Commas
preg_replace(',', '', $myString);
}

Categories