5 Replies - 707 Views - Last Post: 09 February 2010 - 08:45 PM Rate Topic: -----

#1 drewpark88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 08-February 10

Get last 4 digits only in PHP

Posted 09 February 2010 - 05:24 PM

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>";


Is This A Good Question/Topic? 0
  • +

Replies To: Get last 4 digits only in PHP

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3040
  • View blog
  • Posts: 10,620
  • Joined: 08-August 08

Re: Get last 4 digits only in PHP

Posted 09 February 2010 - 06:07 PM

That works for me:
$card = "1234-5678-90AB";
$last4 = substr($card,-4,4);
echo $last4;

Are you sure that you've set your variable ($cc_number)?
Was This Post Helpful? 1
  • +
  • -

#3 drewpark88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 08-February 10

Re: Get last 4 digits only in PHP

Posted 09 February 2010 - 06:33 PM

ok cool just wanted to make sure it will work before I implement it into my project thanks. Oh and yeah I made sure to set the variable thank you :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3040
  • View blog
  • Posts: 10,620
  • Joined: 08-August 08

Re: Get last 4 digits only in PHP

Posted 09 February 2010 - 07:22 PM

A good way to make sure things work is to try them. I usually keep a php file just for testing code so that I can test out snippets before incorporating them into larger projects.
Was This Post Helpful? 1
  • +
  • -

#5 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5365
  • View blog
  • Posts: 27,329
  • Joined: 10-May 07

Re: Get last 4 digits only in PHP

Posted 09 February 2010 - 07:47 PM

To back up what CTphpnwb suggested, you should have a test environment.

2 rules to live by.

1.) Never put test code into productions.
2.) Never allow production code to rely on a test service.
Was This Post Helpful? 1
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3040
  • View blog
  • Posts: 10,620
  • Joined: 08-August 08

Re: Get last 4 digits only in PHP

Posted 09 February 2010 - 08:45 PM

I think my rule would be a corollary of rule one:
Never try to figure out a new (to you) function inside your entire project. Put it in a test file and give it the expected input. When you get it working and feel that you understand it, try it in the project you're developing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1