Jump to content

Phone number UK - Converting string to integer - retain digits.


Go to solution Solved by PaulRyan,

Recommended Posts

Good mornng all,

 

I have a simple contact form which ask for some details from the user including name, number, email, referrer and any comments they may have:

 

<form method="post" action="?page=Contact">
    <fieldset>
        <legend>TSPV-Websites</legend>
        
        <p><label for="name">Name:</label><input type="text" id="name" name="name" value="<?php if(isset($_POST['name'])){ print(htmlspecialchars($_POST['name'])); } ?>" /> <span>* Required</span></p>
        <p><label for="phone">Phone:</label><input type="text" id="phone" name="phone" value="<?php if(isset($_POST['phone'])){ print(htmlspecialchars($_POST['phone'])); } ?>" /> <span>* Required</span></p>
        <p><label for="email">Email:</label><input type="text" id="email" name="email" value="<?php if(isset($_POST['email'])){ print(htmlspecialchars($_POST['email'])); } ?>" /></p>
        <p><label for="referrer">Referrer:</label>
            <select name="referrer" id="referral_list">
                <option name="default">Choose an option...</option>
                <option name="search_engine">Search Engine</option>
                <option name="facebook">Facebook</option>
                <option name="twitter">Twitter</option>
                <option name="friend">Friend</option>
                <option name="other">Other</option>
            </select>
        </p>
        <p><label class="last_label" for="comments">Comments:</label><textarea name="comments" rows="7" cols="35"><?php if(isset($_POST['comments'])){ print($_POST['comments']); } ?></textarea></p>
        <p class="form_btns"><input type="submit" value="Send" /><input type="reset" value="Clear" /></p>
    </fieldset>
    
</form>

 

I want to convert the phone number into an integer but keep losing the preceeding 0's and I've just entered a mobile number example: 07973 762 960 and after I get redirected back to the form (because of an invalid email), it shows only: 7973.

 

Not sure what to do. My database field is set to string for a phone number but I want to change it to int. I want to perform some checks on the phone number to make sure it contains 10 or 11 digits and starts with a 0. I realize there are string functions to check first characters of a string and length but need it to be an integer once sent to database and this issue will arise then.

 

My form processing code is:

//Process the contact form

if(isset($_POST['name']))
{
    foreach($_POST as &$v)
    {
        $v = trim($v);
    }
    
    
    if($_POST['name'] == "" || $_POST['phone'] == "")
    {
        $error = "<span class='error'>Please fill in both required (*) fields.</span>";
    }
    
    $_POST['phone'] = str_replace(array("(",")","-"),"",$_POST['phone']);
    $len = strlen($_POST['phone']);
    
    var_dump($len);
    $_POST['phone'] = (int) $_POST['phone'];
    if(!ctype_digit($_POST['phone']))
    {
        if($len < 10 || $len > 11)
        {
            if(isset($error))
            {
                $error = str_replace("</span>"," ",$error);
                $error .= "<br />Please enter a valid number</span>";
            }
            else
            {
                $error = "<span class='error'>Please enter a valid number.</span>";
            }    
        }
    }
    if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
    {
        if(isset($error))
        {
            $error = str_replace("</span>"," ",$error);
            $error .= "<br />Please enter a valid email</span>";
        }
        else
        {
            $error = "<span class='error'>Please enter a valid email.</span>";
        }        
    }
    
    if(!isset($error))
    {
        //no errors, we have required data - continue
        
    
    }
    
}

 

Thank you for you time and guidance.

 

Kind regards,

 

L2c.

My database field is set to string for a phone number but I want to change it to int.

Don't. Just because a value consists of digits does not mean the value is a number. It should be a string, and preferably without formatting applied to it.

Hi requinix,

 

Thanks for the rapid response.

 

I carried on after making the post and came up with this:

$digits = array('0','1','2','3','4','5','6','7','8','9');
    $len = strlen($_POST['phone']);
    $phone = &$_POST['phone'];
    
    
    for($x = 0; $x < $len; $x++)
    {
        $char = $phone[$x];
        
        if(!in_array($char, $digits))
        {
            $phone = str_replace($phone[$x], "", $phone);
        }
        
        //combats offset issue due to replacing to an empty string shortens length of string.
        $len = strlen($phone);
        
    }
    
    var_dump($_POST['phone']);

 

It returns the string containing just numbers, which is really what I want, just numbers.

 

Are you saying I should just leave it as it is, as they input it?

 

Kind regards,

 

L2c.

  • Solution

Why don't you just use preg_replace?

 

Example:

<?PHP

  $number = '<script>07123456789</script>';
  $number = preg_replace('/[^0-9]/', '', $number);
 
  echo $number;
 
?>
Edited by PaulRyan

preg_replace() to sanitize, ctype_digit to validate.

 

Side note:

$phone = &$_POST['phone'];
It's bad practice to change the contents of $_GET and $_POST in case some code somewhere really did want the original values.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.