UK Phone Number Validation in CodeIgniter

I was looking for a way to validate UK phone numbers in CodeIgniter (or more generally, PHP) and found this on the CodeIgniter forums. Works a treat!

UK Phone Number Validation (regexp trickle!).

For link independence, I’ve copied the pertinent parts from the above link:

So you’d have in your form controller something like:

//Set rules that require numeric only, min length of 9 and field as required.
$this->form_validation->set_rules('phone', 'Phone Number', 'required|numeric|min_length[9]|callback_uk_phone_check');

Then the callback function is:

/**
    * Super quick UK Phone number checker.
    * Adapted from javascript solution found here:
    * http://www.braemoor.co.uk/software/_private/jstelnumbers.js
    **/
    function uk_phone_check($number)
    {
        $errorarr = array(
            //0 => "Valid UK telephone number",
            //1 => "Telephone number not provided",
            //2 => "Please do not include the country code in the telelphone number",
            //3 => "UK telephone numbers should contain 10 or 11 digits",
            4 => "The telephone number should start with a 0",
            5 => "The telephone number is either invalid or inappropriate"
        );
        
        //Set the error to false, our final bit of logic will check against this
        $telnum_error = false;
        
        // Don't allow country codes to be included (assumes a leading "+")
        //$leading = '/^(\+)[\s]*(.*)$/';
        
        // Check that the first digit is 0
        $first = '/^0[0-9]{9,10}$/';
        
        //Check that the telephone number is valid.
        $valid = '/^(01|02|03|05|070|071|072|073|074|075|07624|077|078|079)[0-9]+$/';
        
        // Array holds the regular expressions for the drama telephone numbers
          // see http://stakeholders.ofcom.org.uk/telecoms/numbering/guidance-tele-no/numbers-for-drama
        $drama = array();

        array_push($drama, '/^(0113|0114|0115|0116|0117|0118|0121|0131|0141|0151|0161)(4960)[0-9]{3}$/',
        '/^02079460[0-9]{3}$/', '/^01914980[0-9]{3}$/','/^02890180[0-9]{3}$/','/^02920180[0-9]{3}$/',
        '/^01632960[0-9]{3}$/','/^07700900[0-9]{3}$/','/^08081570[0-9]{3}$/','/^09098790[0-9]{3}$/',
        '/^03069990[0-9]{3}$/');

        //if (preg_match($leading, $number)) {
            //$telnum_error = 2;
        //} elseif (!preg_match($first, $number)) {
        if (!preg_match($first, $number)) {
            $telnum_error = 4;
        } elseif ( !preg_match($valid, $number) ) {
             $telnum_error = 5;
        } else{        
        
        //Check against the drama numbers
            for ($i=0; $i < count($drama); $i++) {
                if ( preg_match($drama[$i], $number) ) {
                    $telnum_error = 5;
                }
            }
        }

        //See if we've got an error
        if($telnum_error){
            //echo "error >>>>>>>>>>>>>>> " . $errorarr[$telnum_error] . " <<<<<<<<<<<<<<<";
            $this->form_validation->set_message('uk_phone_check', 'The %s number must be a valid UK telephone number<br/><u>' .$number. '</u> --> ' . $errorarr[$telnum_error]);
            return FALSE;
        }else    {
            return TRUE;
        }
    } 

Simplify Quoted Strings in VBS

In VBS, strings are always wrapped in double quotes:

WScript.Echo("This is a string")

The output from this code is:

This is a string

Now, if you want (or need) to have a string that itself contains double quotes (for example, if you need to refer to a file path), you need to escape the quotes. To make things complicated, the escape character is a double quote! So what you end up with is:

WScript.Echo("This is a string with some ""quoted text"" in it")

The output from this code is:

This is a string with some "quoted text" in it

If the text that is to be quoted is being concatenated in as a variable, it gets even more confusing, as you need 3 sets of double quotes before and after the variable:

qtd_txt = "quoted text"
WScript.Echo("This is a string with some """+qtd_txt+""" in it")
 

Again, the output from this code is:

This is a string with some "quoted text" in it

So what can be done to simplify this confusing sea of quotes?

The following one-line (ignoring the verbose comments!) function can help:


'Function: cleanQuote
'   Replaces a dummy character in a string with the correct number of quotation
'   marks ("). The dummy character should be one that has no other meaning within
'   the string.
'
'Params:
'   str - the source string containing the dummy characters
'   dummy_char - the dummy character to be replaced
'
'Returns:
'   The original string but with all the dummy characters replaced by quotes
'
'Example usage:
'   tst = "this is a ¬quoted string¬"
'   clean = cleanQuote(tst, "¬")
'       'clean equals: "this is a ""quoted string"""
'
'Version:
'   25 Apr 2011 - Paul Walker - Created
'
Function cleanQuote (str, dummy_char)
    cleanQuote = Replace(str, dummy_char, """")
End Function

I think it’s fairly self-explanatory (it should be with so much comment!), but for sake of completeness, this is how you’d use it for the 3rd example above:

qtd_txt = "quoted text"
txt = "This is a string with some ¬"+qtd_txt+"¬ in it"
final_txt = cleanQuote(txt,"¬")
WScript.Echo(final_txt)

'Function: cleanQuote
'   Replaces a dummy character in a string with the correct number of quotation
'   marks ("). The dummy character should be one that has no other meaning within
'   the string.
'
'Params:
'   str - the source string containing the dummy characters
'   dummy_char - the dummy character to be replaced
'
'Returns:
'   The original string but with all the dummy characters replaced by quotes
'
'Example usage:
'   tst = "this is a ¬quoted string¬"
'   clean = cleanQuote(tst, "¬")
'       'clean equals: "this is a ""quoted string"""
'
'Version:
'   25 Apr 2011 - Paul Walker - Created
'
Function cleanQuote (str, dummy_char)
    cleanQuote = Replace(str, dummy_char, """")
End Function

Again, the output from this code is:

This is a string with some "quoted text" in it

While this might look a bit more complicated, it really isn’t. All we’re actually doing is replacing a pair of double quotes (the escape character followed by the quote we’re displaying) with one arbitrary character. This makes our quoted string much easier to read: in the above example, the otherwise meaningless ¬ character means “a double quote will be displayed here”, and our new little function sorts out the mess.