PHP phone number validation – revisited

Several months ago, when I was first getting familiar with regular expression, I made a post talking about PHP form validation for phone numbers and email addresses.

A couple of days ago, I revisited that post and realized how inefficient my function actually was. The entire point of regex is to recognize multiple patterns within 1 statement. Here is my revised regular expression.

/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i

This single function detects for all the patterns in the original post and more.

Example

$phone_numbers = array( 
    '555-555-5555',
    '5555425555',
    '555 555 5555',
    '1(519) 555-4444',
    '1 (519) 555-4422',
    '1-555-555-5555',
    '1-(555)-555-25555',
);
$regex = "/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i";
foreach( $phone_numbers as $number ) {
    echo $number . ': ' . ( preg_match( $regex, $number ) ? 'valid' : 'invalid' ) . '<br/>';
}

Output

555-555-5555: valid
5555425555: valid
555 555 5555: valid
1(519) 555-4444: valid
1 (519) 555-4422: valid
1-555-555-5555: valid
1-(555)-555-5555: valid
1-(555)-555-25555: invalid

Secure-Shell (SSH) Reference Sheet – Subversion

Hello, all! This is a continuation of my Secure-Shell Reference Sheet series. This one focuses on interacting with subversion (svn) on a remote/local Linux server.

Check out the others:
Secure-Shell (SSH) Reference Sheet – Basic
Secure-Shell (SSH) Reference Sheet – Database

Check the status of all files
svn st

or

svn status
Add files to subversion
svn add *folder/file.php* *another-folder/file2.php*

or

svn add *.php

Note: Using svn add * inside a directory with items already added to the working copy will result in warnings/errors. It’s best to specify the files, or use the following technique:
Continue reading Secure-Shell (SSH) Reference Sheet – Subversion

Secure-Shell (SSH) Reference Sheet – Database

This is the continuation of my Secure-Shell Reference Sheet – Basic.

Note: Anything surrounded by *asterisks* is where your input is required.

Accessing a Database
mysql -u *username* -p *password* -h *hostname* *database*

This will log in directly

mysql -u *username* -h *hostname* *database*

This will prompt you for the database user’s password
Continue reading Secure-Shell (SSH) Reference Sheet – Database

SSH Reference Sheet – Basic

SSH Reference Sheet – basic Secure-shell commands for every day use!

The more I work with SSH, the more I love it! Unfortunately, half the time I forget the commands. This is the first of a series of “Reference Sheets” to help myself and others learn/remember SSH commands.

Note: Anything surrounded by *asterisks* is where your input is required.

Change Directory
cd *directory*
List the contents of a directory
ls *directory*

Continue reading SSH Reference Sheet – Basic

Determine if you are on the Static Posts page in WordPress

When using the Static Home Page option (and thus Static Home Page ) in WordPress, there are ways to check whether you are on the home static page, or the designated posts page.

Static Front Page

// If the current page is the blog posts index,
// but is NOT the site front page
if ( is_home() && is_front_page() ) {
    // do something
}

Continue reading Determine if you are on the Static Posts page in WordPress

Create an HTML element to read it’s attributes in jQuery

Hi everyone! I am working on a WordPress plugin for embedding videos, and I came across needing to read an iframe’s attributes, before it is displayed anywhere. I searched on many forums, the jQuery documentation, and eventually found that you can create an element without it actually being present in the HTML! It’s frustratingly simple:
Continue reading Create an HTML element to read it’s attributes in jQuery

MySQL Unions – getting 2 values from 1 column

Hey everyone! Here is how to retrieve multiple values from the same table. My project had to do with job positions, but I have made the example a little easier to follow:

Let’s say in the usermeta table, each person has a left pocket, and a right pocket. Still with me?

We want to find out if any user has an apple in either pocket. The apple(s) can be in a user’s left pocket, right pocket, or both!
Continue reading MySQL Unions – getting 2 values from 1 column

Reading $_GET values from a URL String in PHP

Reading $_GET values from a URL String in PHP was something I had been interested in for a while. It was quite simple to read the $_GET values from the current page’s URL (obviously), but what if you were using AJAX and passing a URL to figure out what data needed to be passed/used? There’s several other reasons why you may need this, but I digress.

While looking for a way to read $_GET variables from a string in PHP, I stumbled across this forum post on Stack Exchange.

parse_str( parse_url( $url, PHP_URL_QUERY ), $array );

Continue reading Reading $_GET values from a URL String in PHP

Retreive $_GET variables in JavaScript from a URL string

In the case I had found working on a WordPress plugin yesterday, I needed to manipulate and read a URL that was given as a string by a user.

For example, https://www.google.ca/?output=search&q=cats was given and I would want to be able to retrieve what q‘s value was, or if it even existed.

Thanks to Adam Zwakenberg (@AdamZwakk) for showing me this cleaner version.
Continue reading Retreive $_GET variables in JavaScript from a URL string