MainelyDesign.com Blog

2 Neat Little PHP Tricks

Posted on 03/16/2010 at 04:15 pm by Kevin Wentworth
Viewed 12,657 times | 0 comments

In writing a function to validate bank routing numbers, I came across two simple techniques, that I know I will need to use again:

PHP Trick 1: Remove All Characters Except Numbers from a String

A simple little regex does the trick:

  1. $routingNumber = preg_replace('[\D]', '', $routingNumber);

PHP Trick 2: Loop Through a String Character by Character

Setting two variables in a for loop does the trick (that could be trick number 3...):

  1. for ($i = 0, $j = strlen($routingNumber); $i < $j; $i++ ) {
  2.     echo $routingNumber[$i]; //access to each character in a string
  3. }

Cheers,
-Kevin Wentworth

Bookmark and Share

Tags for 2 Neat Little PHP Tricks

Php | Example | Tutorial | Web Programming

Comments for this Posting

No comments. Be the first to post a reply.

Sorry, comments are closed for this posting.

Please Email Kevin if you have any questions. Thanks!

Meet Site Avenger - Hosted Content Management System

Powered By: Site Avenger | Site Production: Saco Design