Stripos issues in php4

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;    
                }

Answers


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.


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


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 */ 

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));
}   

Need Your Help

What is the easiest way to get an embedded upload progress bar using Ruby/Sinatra/Haml/Passenger/nginx?

progress-bar sinatra haml

I have a website where people can upload 30+mb of data in a single block, and I want to be able to show them the progress of their upload without causing the web page to become unresponsive, simila...