Question

I want to able connecting to my website with the first name and the last name or the email (and of course with a password). The problem is a that in the database the first name and the last name aren't in the same column. My question is how, using php, to check if the $_post have an a email address and if not to take the two words and make a array with the 2 names.

Était-ce utile?

La solution 2

Concerning the email filter : PHP has validation filters.

You can find more informations here : http://www.php.net/manual/en/filter.filters.validate.php

In your case, you want to use the FILTER_VALIDATE_EMAIL :

if(filter_var($email, FILTER_VALIDATE_EMAIL))
{ ... }

Concerning the part with the two ords, use the function explode.

Syntax of the explode function :

array explode ( string $delimiter , string $string)

In your case :

$array = explode(' ', $string);

Autres conseils

You can use regular expression to check whether or not the input is an email or the user's name.

//Regular expression for email addresses
$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/'; 

        $password = $_POST['password'];

//Check if the input is an email or first and last name
if(preg_match($regex, $_POST['input'], $match)){
        $email = $_POST['input'];
}else{
        //Assuming the user entered "Firstname Lastname"
        list($firstname,$lastname) = explode(" ", $_POST['input']);        
}

If you've got a string with words then you can just use the explode() function to get each word as an array value like:

<?php
    $str = "Alpha Beta Gamma";
    $str_array = explode(" ",$str);
    echo $str_array[0]; //Alpha
    echo $str_array[1]; //Beta
    echo $str_array[2]; //Gamma
?>

Maybe this is what you searching for, but your question is not clear.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top