check string contents: can be used as var?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Oliver Saunders

    check string contents: can be used as var?

    is there anyway to check if a string could be used a variable name. So
    return false if the first character isn't a letter or if it contains
    spaces etc.

    I started writing a function that would do a load of tests because I
    couldn't see any easier way of doing it:

    <?
    function is_valid_handle ($str) {
    // determines if the string provided can be used as a handle
    // such as a php variable or html/css class
    if (strlen($str) > 255)) return false;
    if (strpos($str,' ')) return false;
    // incomplete, needs more tests
    return true;
    }
    ?>

    but as soon as i realised i would have to use range() to check all the
    characters were valid and all that i decided there must be an easier
    way. Do you guys know of one or am I just going to have to write it myself?

    It was about then I had a great idea maybe if you use the variable
    variable syntax you can see if it will return false but unfortunately it
    does this:

    <?
    $foo= '5very @#bad variable name';
    $$foo = 'Hi!'; // doesn't cause error
    echo $$foo; // outputs Hi!
    echo $5very @#bad variable name; // causes an error, obviously
    ?>

    Seems PHP will quite happily let you store data in a variable of
    whatever you like. I guess that's a nice feature. So...any ideas?
  • Steve

    #2
    Re: check string contents: can be used as var?

    [color=blue]
    > is there anyway to check if a string could be used a variable name. So
    > return false if the first character isn't a letter or if it contains
    > spaces etc.[/color]

    See <http://www.php.net/language.variab les> which provides a regular
    expression to validate variabe identifiers.

    ---
    Steve

    Comment

    • Oli Filth

      #3
      Re: check string contents: can be used as var?

      Oliver Saunders said the following on 18/10/2005 18:21:[color=blue]
      > is there anyway to check if a string could be used a variable name. So
      > return false if the first character isn't a letter or if it contains
      > spaces etc.
      >[/color]

      Just out of curiosity, why do you want to do this?

      --
      Oli

      Comment

      • Oliver Saunders

        #4
        Re: check string contents: can be used as var?

        > Just out of curiosity, why do you want to do this?


        I'm seeing if I can create my own form abstraction classes. When you do
        something like this:

        $surname = new osisField;
        $surname->SetName('surna me');

        I want setname to SetName() to be able to check to see if its parameter
        can be used as a valid <input name=""> and post variable.

        Thinking about it you can use any key for an array can't you. So
        $_POST['45##'] is a valid key but then you can't extract() that. (See
        test below)

        <?
        $foo = array('##56' => 'data'); // valid
        echo $foo['##56']; // valid
        echo extract($foo); // returns 0 variables extracted
        echo $bar = '##56';
        echo $$bar; // unsurprisingly undefined
        ?>

        Comment

        Working...