Jump to content

Recommended Posts

I'm trying to write a script to validate a legal php variable name, but I can't seem to make it work so it doesn't validate spaces.  Any help would be much appreciated!

 

Here's my code:

 

    
<fieldset><legend><h1>PHP Variable Validator</h1></legend>
    
    <p>A valid PHP variable begins with the dollar sign ("$") followed by either a letter or underscore ("_") followed by any series of numbers, letters or underscores.</p>
    <p>Ex: $variable</p>
<form action="name_validator.php" method="post">
      <input name="variable" type="text" />
      <input name="submit" type="submit" value="Validate it!" />
    </form>


<?php
$variable = $_POST['variable'];

if ($variable == "quit") {
echo "Thanks for trying the validator!";
}
elseif (preg_match("/^[$][a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/",$variable)) {
   echo "<b>" . $variable . "</b>" . " is a valid PHP variable name";
} 
elseif (!isset($variable)) {
   	echo ""; 
} 
else {
 echo "<b>" . $variable . "</b>" . " is not a valid PHP variable name";
}

?>

</fieldset>

Link to comment
https://forums.phpfreaks.com/topic/261789-validate-user-entry-to-have-no-spaces/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.