0

Please i have the following function for a contact form, but it shows the following error "Fatal error: Call to undefined function: stripos() in" how can i fix it

function checkEmail($vEmail) {   

                    $invalidChars ="/:,;" ; 
                    if(strlen($vEmail)<1) return false;                                         //Invalid Characters
                    $atPos = stripos($vEmail,"@",1);                                    //First Position of @
                    if ($atPos != false) $periodPos = stripos($vEmail,".", $atPos);         //If @ is not Found Null . position
                    for ($i=0; $i<strlen($invalidChars); $i++) {                            //Check for bad characters 
                        $badChar = substr($invalidChars,i,1);       //Pick 1
                        if(stripos($vEmail,$badChar,0) != false)    //If Found
                            return false;
                    }
                    if ($atPos == false)                            //If @ is not found
                        return false;       
                    if ($periodPos == "")                           //If . is Null
                        return false;
                    if (stripos($vEmail,"@@")!=false)               //If @@ is found
                        return false;
                    if (stripos($vEmail,"@.") != false)             //@.is found
                        return false;
                    if (stripos($vEmail,".@") !=  false)            //.@ is found
                        return false;

                    return true;    
                }
oezi
  • 51,017
  • 10
  • 98
  • 115

4 Answers4

3

as you can see from the documentation, stripos() only exists in PHP5. Anyways, your code doesn't need to chack case-insensitive because it only checks for . @ / : , ; - so you can just replace stripos() with strpos().

you could also add an own stripos() to your codebase, wich could look like thoe following (using strtolower() and function_exists()):

if(!function_exists("stripos")){
  function stripos($haystack, $needle, $offset = 0){
    return strpos(strtolower($haystack), strtolower($needle), $offset)
  }
}

note that this is a very basic replacemend and might not give the same result like a real stripos() in each end every case. it's valid for basic usage, but i havn't done broad tests.

oezi
  • 51,017
  • 10
  • 98
  • 115
  • +1 even if the case insensitivity is needed, the arguments to `strpos()` can just be passed through `strtolower()` and you essentially have `stripos()`. – MrCode Jun 11 '12 at 14:05
  • @MrCode: i thougth exactly the same and just added an example using `strtolower` to built an own `stripos`. – oezi Jun 11 '12 at 14:07
0

stripos is a PHP 5 function, you need to upgrade.

Lee Davis
  • 4,685
  • 3
  • 28
  • 39
0

stripos() is not available in PHP 4

manual

stripos

(PHP 5)

stripos — Find the position of the first occurrence of a case-insensitive substring in a string

one of the comments in the manual shows this code, which might help you out:

if(!function_exists("stripos")){
    function stripos(  $str, $needle, $offset = 0  ){
        return strpos(  strtolower( $str ), strtolower( $needle ), $offset  );
    }/* endfunction stripos */
}/* endfunction exists stripos */

if(!function_exists("strripos")){
    function strripos(  $haystack, $needle, $offset = 0  ) {
        if(  !is_string( $needle )  )$needle = chr(  intval( $needle )  );
        if(  $offset < 0  ){
            $temp_cut = strrev(  substr( $haystack, 0, abs($offset) )  );
        }
        else{
            $temp_cut = strrev(    substr(   $haystack, 0, max(  ( strlen($haystack) - $offset ), 0  )   )    );
        }
        if(   (  $found = stripos( $temp_cut, strrev($needle) )  ) === FALSE   )return FALSE;
        $pos = (   strlen(  $haystack  ) - (  $found + $offset + strlen( $needle )  )   );
        return $pos;
    }/* endfunction strripos */
}/* endfunction exists strripos */ 
Bono
  • 4,757
  • 6
  • 48
  • 77
0

As mentioned it's PHP5 only...

But that is a nasty email validation function, try this!

function validateEmail($email)
{
    return(preg_match("/^([a-zA-Z0-9_\.\-+])+\@([a-zA-Z0-9\-])+(\.[a-zA-Z0-9]{2,7})+$/", $email));
}   
Brian
  • 8,418
  • 2
  • 25
  • 32
  • That doesn't work for the valid email address timmy.o'toole@example.com, among others. The best way I've found to validate is to use Dominic Sayers is_email function http://isemail.info/ – Nick Jun 11 '12 at 14:10
  • @Brian regex email validation does more harm than good, you'll end up losing genuine users or customers which don't match the pattern, for the sake of stopping some spam. If you need to know if the email is good, send a confirmation/activation link to it. – MrCode Jun 11 '12 at 14:16
  • This is old but what if he is a student and cant send emails through the school server? I'm in this situation. – jason dancks Nov 26 '12 at 18:22