Skip to content

Instantly share code, notes, and snippets.

@edward-jimenez
Created May 6, 2018 22:59
Show Gist options
  • Save edward-jimenez/f37e117a3692faa99e80faf9681fc556 to your computer and use it in GitHub Desktop.
Save edward-jimenez/f37e117a3692faa99e80faf9681fc556 to your computer and use it in GitHub Desktop.
Example on using PHP substr_replace function
<?php
/*
Print the last 4 charactrs of a credit card number replacing the first 12 characters with asterisks
using the PHP substr_replace function
*/
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Replace String</title>
</head>
<body>
<?php if($_SERVER['REQUEST_METHOD'] == 'GET') { ?>
<form method="POST" action="replace-string.php" >
<input type="text" name="credit_card" placeholder="Credit Card" size="16" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php } else {
/* Returns the credit card variable with the first 12 digits replaced with 'x'
substr_replace(<original_string>, <replacement_string>, <start>, <length>);
*/
$last_four = substr_replace($_POST['credit_card'], 'xxxx xxxx xxxx ', 0, strlen($_POST['credit_card']) - 4 );
echo "The last four of your credit card number is " . $last_four;
} ?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment