Solved

# help with substr

Posted on 2008-06-17
263 Views
Hi

I'm trying to work on a check digit routine and need to do 3 things
1) find the length of the variable \$paymentReference1
2) set the variable \$_toCheck to be all the digits except the last one
3) set the variable \$_toMatch to be the last digit

Not sure if the code I have attached is the best way to do this or could it be simpler. Will my code even work?

Neil
``````\$paymentReference1 = "10948055";

\$_len = strlen(\$paymentReference1)-1;
\$_toCheck = substr(\$toCheck, 1, \$_len);
\$_toMatch = substr(\$toCheck, \$_len, 1);
``````
0
Question by:Neil Thompson
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 49

Expert Comment

ID: 21806993
Looks fine, 3) can be
\$_toMatch = substr(\$toCheck, -1);
0

LVL 49

Expert Comment

ID: 21806996
Actually, 2 is incorrect. Should be
\$_toCheck = substr(\$toCheck, 0, \$_len);
0

LVL 48

Accepted Solution

hernst42 earned 500 total points
ID: 21807007
Just try:

\$toCheck = \$paymentReference1;
\$_toCheck = substr(\$toCheck, 0, -1);
\$_toMatch = substr(\$toCheck, -1);
0

LVL 3

Author Closing Comment

ID: 31468124
Thanks
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
###### Suggested Courses
Course of the Month6 days, 8 hours left to enroll