Checking if string contains characters other than a-Z?

Discussion in 'PHP' started by Shadow, Apr 11, 2006.

  1. #1
    I'm looking for an efficient way to check a string for special characters other than checking every character in the string separately :eek:

    Could someone please help me out with this one?
     
    Shadow, Apr 11, 2006 IP
  2. Selkirk

    Selkirk Peon

    Messages:
    93
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Use the function ctype_alpha(), or rather !ctype_alpha(). Sorry, no link. I'm a peon.
     
    Selkirk, Apr 11, 2006 IP
  3. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #3
    if (preg_match ('/[^a-z]/i', $string_to_check)) {
        // The string contains characters other than a-z and A-Z
    }
    PHP:
     
    exam, Apr 11, 2006 IP
    Shadow likes this.
  4. Shadow

    Shadow Peon

    Messages:
    191
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks exam, exactly what I was looking for :D
     
    Shadow, Apr 11, 2006 IP
  5. ServerUnion

    ServerUnion Peon

    Messages:
    3,611
    Likes Received:
    296
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Here is the function that I use to produce friendly URL's, hope it helps...

    
    function make_url_friendly($url)
    {
        $url = strtolower($url);
        $find = array(' ',
                '&',
                '\r\n',
                '\n',
                '/',
                '\\',
                '+');
        $url = str_replace ($find, '-', $url);
        $find = array(' ',
                'é',
                'è',
                'ë',
                'ê');  
        $url = str_replace ($find, 'e', $url);
        $find = array(' ',
                'ó',
                'ò',
                'ô',
                'ö');  
        $url = str_replace ($find, 'o', $url);
        $find = array(' ',
                'á',
                'à',
                'â',
                'ä');
        $url = str_replace ($find, 'a', $url);  
        $find = array(' ',
                'í',
                'ì',
                'î',
                'ï');
        $url = str_replace ($find, 'i', $url);
         $find = array(' ',
                'ú',
                'ù',
                'û',
                'ü');
        $url = str_replace ($find, 'u', $url);  
        $find = array('/[^a-z0-9\-<>]/',
                '/[\-]+/',
                '/<[^>]*>/');
        $repl = array('',
                '-',
                '');
        $url =  preg_replace ($find, $repl, $url);
        return $url;
    }
    
    Code (markup):
     
    ServerUnion, Apr 11, 2006 IP
  6. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You're welcome

    @Serverunion You can do that a lot easier- look up strtr().
     
    exam, Apr 11, 2006 IP
  7. ServerUnion

    ServerUnion Peon

    Messages:
    3,611
    Likes Received:
    296
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Prolly, but this is a solution for them, not just a pointer in the right direction. ;)
     
    ServerUnion, Apr 12, 2006 IP
  8. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I thought you said it was a function you use. I was just suggesting you could do it better/faster. :)
     
    exam, Apr 12, 2006 IP
  9. geilt

    geilt Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks much,

    Was exactly what i was looking for.

    Works good with Explode!!!

    if (preg_match("/\\s/", $_POST['FirstName']) && is_null($_POST['LastName'])) {
    $fullname = explode(" ", $_POST['FirstName']);
    $firstname = $fullname[0];
    $lastname= $fullname[1];
    }
    else
    {
    $firstname = $_POST['FirstName'];
    $lastname = $_POST['LastName'];
    }

    Note, I put the && is_null lastname to make sure someone smart didn't put a space at the end of their first name causing it to trigger. and putting the last name blank.
     
    geilt, Apr 26, 2010 IP