-1

I'm quite new to PHP and don't have as much experience with is as I do with other languages. I have the following form, and what I would like to do is to check if the firstname field begins with an uppercase, if the nickname field is lowercase, and if the lastname field is all uppercase. I've seen people turn strings and text into different cases but I don't know how to do the validation that I hope to achieve.

I've turned text into lower/upper/case but that's just half a line of very simple code from W3CSchools.

Also this isn't a duplicate of any other question. Others focus on pure PHP functions like creating a string and echoing a result, the other question doesn't even apply to what I'm trying to achieve, so there's my edit for that.

Here is the HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>Testing case sensitive validations</title>
    </head>
    <body>
        <form name="newform" method="post" action="walkin.php">
        Firstname <input type="text" name="fname"><br><br>
        Nickname <input type="text" name="nname"><br><br>
        Lastname <input type="text" name="lname"><br><br>
        <input type="submit" name="submit" value="Submit">
        </form>
    </body>
</html> 

Here is the PHP I have so far:

<?php
if (isset($_POST['submit'])) {
        $fname = $_POST['fname'];
        $nname = $_POST['nname'];
        $lname = $_POST['lname'];
    }
?>
  • I'm also a bit confused as to why StackOverflow removed the feature to highlight your code and do ctrl + K to add four spaces on each line, why did they get rid of this shortcut? – Bamboozler Apr 08 '19 at 09:44
  • 2
    They didn't it still works like that – RiggsFolly Apr 08 '19 at 09:45
  • How would you do this in another language you're more proficient in? Also, please share what you said you tried. – Federico klez Culloca Apr 08 '19 at 09:46
  • firstly: Avoid w3schools for learning PHP. It's riddled with vaguness and bad practice. It says PHP5 tutorial .. which version? 5.4 is very different from 5.6.. plus, learn 7. secondly: Indenting will help you read code quicker. thirdly: research regex – treyBake Apr 08 '19 at 09:46
  • 1
    As with all new languages, the place to start is [The PHP Manual, string functions](https://www.php.net/manual/en/ref.strings.php) and while you search for something to help with this problem you will learn 20 other things – RiggsFolly Apr 08 '19 at 09:49

4 Answers4

3

The answer is surprisingly simple. You can use a combination of strtoupper, strtolower and substr with strict comparing.

$firstLetterUppercase = 'A_word';

if(substr($firstLetterUppercase, 0, 1) === strtoupper(substr($firstLetterUppercase, 0, 1))) {
    // first letter is upper case
}

$nickNameLowerCase = 'nickname';

if($nickNameLowerCase === strtolower($nickNameLowerCase)) {
    // it's lower case
}

$lastName = 'ALL UPPER CASE';

if($lastName === strtoupper($lastName)) {
    // it's upper case
}

If you mix and match them you can get just about anything done.

Do note that there's no difference between loose and strict compare here, but it's always nice to use strict.

Andrei
  • 3,434
  • 5
  • 21
  • 44
2
  1. To check the first letter with upper case try below

    preg_match("/^[A-Z]/", 'Bamboozler');// This will return true if first letter is upper case
    
  2. To check all the character of a string lowercase, you can use ctype_lower() function

    ctype_lower('bamboozler');// This will return true if all the character are lower cases.
    
  3. To check all the character of a string uppercase, you can use ctype_upper() function

    ctype_upper('BOOM');// This will return true if all the character is upper cases.
    
Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
1

A general case check function.

No need to use preg_match() or substr(). You can directly access the first character using the string index method of {0}. If you wanted the second character, use {1}.

function checkFirstLetter($str,$upper=true) {
  if ($upper) {
    return ($str{0} == strtoupper($str{0}));
  } else {
    return ($str{0} == strtolower($str{0}));
  }
}

$test[] = array("str" => "test", "case" => true);
$test[] = array("str" => "Test", "case" => true);
$test[] = array("str" => "test", "case" => false);
$test[] = array("str" => "Test", "case" => false);
$test[] = array("str" => "123", "case" => true);
$test[] = array("str" => "123", "case" => false);
$test[] = array("str" => "!@#", "case" => true);
$test[] = array("str" => "!@#", "case" => false);

foreach($test as $k => $v) {
  echo 'str:['.$v['str'].'] 
    upper:['.$v['case'].'] 
    result:['.checkFirstLetter($v['str'],$v['case']).']<br />';
}

And the results:

str:[test] upper:[1] result:[]
str:[Test] upper:[1] result:[1]
str:[test] upper:[] result:[1]
str:[Test] upper:[] result:[]
str:[123] upper:[1] result:[1]
str:[123] upper:[] result:[1]
str:[!@#] upper:[1] result:[1]
str:[!@#] upper:[] result:[1]

Edit: I really like the ctype_upper($str) and ctype_lower($str) option, but you will need to have that extension installed and I do not. If you do have this extension, you could do:

if (ctype_upper($str{0})) {
  // is upper first
}

There is also ucfirst() and ucwords() - both may help based on your question if you want to force a syntax standard on user input without the need to error report.

Tigger
  • 8,980
  • 5
  • 36
  • 40
0

This will return true if first character is uppercase. while $str is the string you want to check.

preg_match("/^[A-Z]/", $str );

you can do like this if you need to convert whole string to uppercase,

if( preg_match("/^[A-Z]/", $str ) ){
$str = strtoupper("$str");
echo $str;
}

similarly you can convert string to lowercase using strtolower()

Vishwa
  • 801
  • 11
  • 26