954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Get only 4-digits with PHP

I have a quick question. How do I get only the last 4 digits of the cc_number? Will the following work? Or should I try something different..

Oh and I am emailing to myself that's why it has $message.

Here's the code I think will work:

$message .= "<td>{$_GET['cc_number'] = "XXXX-XXXX-XXXX-" . substr($cc_number,-4,4)}</td>";
drewpark88
Junior Poster
177 posts since Feb 2010
Reputation Points: 45
Solved Threads: 5
 
$a = substr($_GET['cc_number'],-4,4);
echo $a;
phpbeginners
Posting Whiz in Training
226 posts since Jul 2009
Reputation Points: 12
Solved Threads: 32
 

Inside a complex syntax brackets ( {$var} ) you can call any function with a return value that can be converted to casted to a string. So using your code that would be:

$message .= "<td>XXXX-XXXX-XXXX-{substr($_GET['cc_number'] , -4, 4)}</td>";

//Or this....
$message .= "<td>XXXX-XXXX-XXXX-".substr($_GET['cc_number'] , -4, 4)."</td>";

//Or even this...
$message .= "<td>XXXX-XXXX-XXXX-";
$message .= substr($_GET['cc_number'] , -4, 4);
$message .= "</td>";



The first or second ones make most sense to me, so I would chose one of them, but it's totally up to you!

FlashCreations
Posting Whiz
395 posts since Sep 2008
Reputation Points: 47
Solved Threads: 47
 

Thanks for the help FlashCreations! I appreciate it :)

Inside a complex syntax brackets ( {$var} ) you can call any function with a return value that can be converted to casted to a string. So using your code that would be:

$message .= "<td>XXXX-XXXX-XXXX-{substr($_GET['cc_number'] , -4, 4)}</td>";

//Or this....
$message .= "<td>XXXX-XXXX-XXXX-".substr($_GET['cc_number'] , -4, 4)."</td>";

//Or even this...
$message .= "<td>XXXX-XXXX-XXXX-";
$message .= substr($_GET['cc_number'] , -4, 4);
$message .= "</td>";
The first or second ones make most sense to me, so I would chose one of them, but it's totally up to you!
drewpark88
Junior Poster
177 posts since Feb 2010
Reputation Points: 45
Solved Threads: 5
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You