Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    nee
    nee is offline
    New Coder
    Join Date
    Feb 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Trouble Wiriting A Regex Funcion To Check Variables Agsinst

    How can I write a function that I can use over and over againt to check against variables to see if they meet a specific regex?

    For example how could I take something like

    PHP Code:
    function valid()
    {
     return 
    preg_match('/^[A-Z ]');

    But write the function in a way so any where through out my script I can use it to check any random variable to see if that variable matches the regex?

    Say I have multiple variables that have text in them, and I want to make sure they start with a capital letter like in the regex above how could I check he function against the variable?


    I was thinking it would be something like

    PHP Code:
    if ($text->valid()) {
    echo 
    "good to go";

    Am I explaining myself clear enough?

    I've seen function written to check specific variables, but I want a functiuon that I can use over and over and check against any given variable i decide. How can I do this?
    Last edited by nee; 02-04-2013 at 05:15 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I don't understand your question at all. You have it almost spot on in the function you've written, you just need to accept an argument into that function and return a result.
    PHP Code:
    function valid($arg)
    {
        return 
    preg_match('/^[A-Z ]/'$arg);

    If you are using an object, you simply refer to data within that object:
    PHP Code:
    public function valid()
    {
        return 
    preg_match('/^[A-Z ]/'$this->property);

    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    nee
    nee is offline
    New Coder
    Join Date
    Feb 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Fou-lu what is the $arg variable exactly for? Is that what I am checking with? If it matches $arg then it matches the function?

    What I mean is how do i check any variable againt the function to see if it matches the regex?

    Say I have three variables like

    PHP Code:
    $one $_POST['one'];
    $two $_POST['two'];
    $three $_POST['three']; 
    Then I want to choose any one of those variable and check them against the valid() function. How can I do that?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You pass it into the function:
    PHP Code:
    if (valid($_POST['one']))
    {
        print 
    'That begins with an uppercase letter or a space.';

    You can also use array_filter with it and remove anything that doesn't match:
    PHP Code:
    $array = array(
        
    'The cat and the hat',
        
    '1403',
    );

    function 
    valid($arg)
    {
        return 
    preg_match('~^[A-Z ]~'$arg);
    }  

    $aValid array_filter($array'valid');
    print_r($aValid); 
    Would give me:
    Code:
    Array ( [0] => The cat and the hat )
    And the $_POST is simply an array as well, so that can be used directly.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    nee (02-04-2013)

  • #5
    nee
    nee is offline
    New Coder
    Join Date
    Feb 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I've been tinkering with a simple script just seeing what I can make happen. Right now it is very rough, and eventually I will have one function to do both steps, but for now I'm getting the code working and plan to polish as I build.
    What I'm trying to do is detect if something starts with a capital letter. If it does then nothing really happens, but it if does not start with a capital I want the first letter to be made capital.

    The below code all works except for line 15. The else statement with the preg_replace.

    PHP Code:
    $bookTitle $_POST['bookTitle'];

    function 
    valid($arg)
    {
        return 
    preg_match('/^[A-Z]/'$arg);
    }  

    if (
    valid($bookTitle))
    {
        
    $title $bookTitle;
    }  

    else {
        
    $title preg_replace_callback('/^[A-Z]/'$bookTitle);
    }

    echo 
    $title
    How can I convert the letter from lower case to upper case in a simple, but effective way?

    And as of right now if something starts with a space the script passes. How can I make it so only if the first character is an upper case the script passes?

  • #6
    New Coder
    Join Date
    Dec 2012
    Location
    USA
    Posts
    82
    Thanks
    3
    Thanked 17 Times in 17 Posts
    PHP Code:
    return preg_match('/^[A-Z]/'substr(trim($arg), 01)); 
    trim() removes leading and trailing spaces, substr(), well, it explains itself.

    ucfirst() will change the first character of a string to uppercase.

  • #7
    nee
    nee is offline
    New Coder
    Join Date
    Feb 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I'm still a litle weary on what $arg is for. What does that do in the function? I understand it is an argument for the function to work, but what does it do and what do I use it for?

  • #8
    New Coder
    Join Date
    Dec 2012
    Location
    USA
    Posts
    82
    Thanks
    3
    Thanked 17 Times in 17 Posts
    PHP Code:
    function valid($arg){
        return 
    preg_match('/^[A-Z]/'$arg);

    The variable $arg is abstract, it is only available locally - meaning you can only access it within the function valid().

    PHP Code:
    $arg2 'Hello';
    function 
    valid($arg){
        return 
    preg_match('/^[A-Z]/'$arg);
    }
    valid($arg2); 
    $arg is the same thing, a variable, as $arg2; the only difference is that $arg2 is global as opposed to $arg which is local to valid().

    You use $arg to pass information to the function valid(); $arg could be $imaverylongimpracticalvariablename for all we care.

    PHP Code:
    function valid($imaverylongimpracticalvariablename){
        return 
    preg_match('/^[A-Z]/'$imaverylongimpracticalvariablename);

    Basically $arg is a variable. A variable that can only be access by valid()

  • #9
    nee
    nee is offline
    New Coder
    Join Date
    Feb 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I knew it was local and not global. I just didn't get it was kind of a place holder.

    The code you posted above, but I can't seem to get my lower case first letter to go to an upper case letter.

  • #10
    New Coder
    Join Date
    Dec 2012
    Location
    USA
    Posts
    82
    Thanks
    3
    Thanked 17 Times in 17 Posts
    If you copied as is, then it wouldn't do that.

    You need to implement ucfirst() on the string that you want to have an uppercase first letter

  • #11
    nee
    nee is offline
    New Coder
    Join Date
    Feb 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I've made a very basic function that takes a string, makes the string all lower case, and then capitalizes the first letter.

    PHP Code:
    $bookTitle $_POST['bookTitle']; 

    function 
    nameize($str) {   
        
    $string strtolower($str);
        return 
    ucfirst(trim($string));
        }
     

    echo 
    nameize($bookTitle); 
    I'm just doing this as a learning experiment. I was wondering what I could do to improve this? Should I implememnt some utf-8 stuff? Would it be difficult to make the function multi-language?
    Last edited by nee; 02-05-2013 at 06:49 PM.

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    UTF-8 isn't natively handled in PHP until (hopefully) 6.0. So in order to make use of multibyte charsets, you'd need to use the mb_string library functions. PCRE handles multibyte, so I guess you could chain that to a preg_replace_callback and manipulate the strings using mb_* functions. There's no mc_ucfirst function, so you'd need to write one by using mb_strlen, mb_substr, and mb_strtoupper.

    Also, you can look at using filter_var functionality and giving it a callback such as the function you have above. It would need modification in order to handle the check for the first character as upper case, but it's job is to return the filtered string as changed, and remove the ones that don't match. That can be used with filter_input_array and can be customized to each item expected.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #13
    nee
    nee is offline
    New Coder
    Join Date
    Feb 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    What are the advantages to using utf-8? I'm not too familiar with mb functions. Is utf-8 for multi language stuff?

  • #14
    New Coder
    Join Date
    Dec 2012
    Location
    USA
    Posts
    82
    Thanks
    3
    Thanked 17 Times in 17 Posts
    If you are not familular with character sets, you NEED to read this:

    http://www.joelonsoftware.com/articles/Unicode.html

  • #15
    nee
    nee is offline
    New Coder
    Join Date
    Feb 2013
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks, TFlan that article explained a lot.

    I stole the two functions below off of php.net http://www.php.net/manual/en/functio...rst.php#108856

    PHP Code:
    $bookTitle $_POST['bookTitle']; 

    function 
    substr_unicode($str$s$l null) {
        return 
    join(""array_slice(
            
    preg_split("//u"$str, -1PREG_SPLIT_NO_EMPTY), $s$l));
    }

    function 
    my_ucfirst($string$e ='utf-8') {
            
    $orig_string mb_strtolower($string);
            if (
    function_exists('mb_strtoupper') && function_exists('mb_substr') && !empty($string)) {
                
    $string mb_strtolower($string$e);
                
    $upper mb_strtoupper($string$e);
                
    preg_match('#(.)#us'$upper$matches);
                
    $string $matches[1] . mb_substr($string1mb_strlen($string$e), $e);
            } else {
                
    $string ucfirst($string);
            }
           
            
    $string1 substr_unicode($string01);
            
    $string2 substr_unicode($orig_string1);
            
    $string $string1 $string2;
           
            return 
    $string;
        } 

    echo 
    my_ucfirst($bookTitle); 
    I added mb_strtolower() to line 10 so that the variable $string is first made all lower case before the first letter is changed to a capital letter.

    I don't want this thread to get much longer becuase it is turning into a bunch of random pieces of information. I'm just thinking of what else i could do to improve the second function that actually changes the characters. I want to use trim() because if my string starts with a space then the first actual letter remains lower case. Would the function trim() be the right function to use? Or is there an mb function I should use instead?
    Last edited by nee; 02-05-2013 at 09:40 PM.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •