PHP uppercase letter in String - php

Like the title says, I'm looking for a way to check if a string contains an uppercase letter in it. It is for a password field, and I cannot use regex because we have not learned any of that yet in class.
I tried to use ctype_upper but that only seems to work if every character in the string is uppercase.
Is there a way to check any character in a string, but not using regex?

You can try this:
if (strtolower($string) != $string) {
echo 'You have uppercase in your string';
} else {
echo 'You have no uppercase in your string';
}
This checks if the converted string to lowercase is equal to the original string. Hope this helps...

Try this..
Use the strtoupper() function to transform the string into all uppercase characters that’s capitalized letters, and then compare the transformed string against the original one to see if they are identical. If they are, then you are pretty sure the original string was also a string consisting of ONLY capital letters
if (strtoupper($string) == $string) {
echo $string.' is all uppercase letters.';}

function isPartUppercase($string) { if(preg_match("/[A-Z]/", $string)===0) { return true; } return false; }
The function uses a simple regular expression that tries to find any upper case A-Z characters, preg_match() returns the number of instances of the expression it finds in the string, but stops at 1, preg_match_all() returns the count of all instances it finds.

Related

How to change specific first Letter in string to Capital using PHP?

If the first character of my string contains any of the following letters, then I would like to change the first letter to Uppercase: (a,b,c,d,f,g,h,j,k,l,m,n,o,p,q,r,s,t,v,w,y,z) but not (e,i,u,x).
For example,
luke would become Luke
egg would stay the same as egg
dragon would become Dragon
I am trying to acheive this with PHP, here's what I have so far:
<?php if($str("t","t"))
echo ucfirst($str);
else
echo "False";
?>
My code is simply wrong and it doesn't work and I would be really grateful for some help.
Without regex:
function ucfirstWithCond($str){
$exclude = array('e','i','u','x');
if(!in_array(substr($str, 0, 1), $exclude)){
return ucfirst($str);
}
return $str;
}
$test = "egg";
var_dump(ucfirstWithCond($test)); //egg
$test = "luke";
var_dump(ucfirstWithCond($test)); //Luke
Demo:
http://sandbox.onlinephpfunctions.com/code/c87c6cbf8c616dd76fe69b8f081a1fbf61cf2148
You may use
$str = preg_replace_callback('~^(?![eiux])[a-z]~', function($m) {
return ucfirst($m[0]);
}, $str);
See the PHP demo
The ^(?![eiux])[a-z] regex matches any lowercase ASCII char at the start of the string but e, u, i and x and the letter matched is turned to upper inside the callback function to preg_replace_callback.
If you plan to process each word in a string you need to replace ^ with \b, or - to support hyphenated words - with \b(?<!-) or even with (?<!\S) (to require a space or start of string before the word).
If the first character could be other than a letter then check with an array range from a-z that excludes e,i,u,x:
if(in_array($str[0], array_diff(range('a','z'), ['e','i','u','x']))) {
$str[0] = ucfirst($str[0]);
}
Probably simpler to just check for the excluded characters:
if(!in_array($str[0], ['e','i','u','x'])) {
$str[0] = ucfirst($str[0]);
}

validate a string consiting only of letters, numbers and optional spaces

I have this function:
function validate_string_spaces_only($string) {
if(preg_match("/^[\w ]+$]/", $string)) {
return true;
} else {
return false;
}
}
I want to match a string that consists only of letters and numbers with an optional space character. When I feed the above function a string containing only letters, numbers and spaces it fails every time. What am I missing?
You have an extra ] character in your regex near the end. Remove it and it should work.
"/^[\w ]+$]/" should be "/^[\w ]+$/".
(Also note that \w typically allows underscores as well, which you may or may not want.)
This regex will:
match a string that consists only of letters and numbers with an optional space character
^[a-zA-Z0-9\s]+$

php validate string with preg_match

I am trying to verify in PHP with preg_match that an input string contains only "a-z, A-Z, -, _ ,0-9" characters. If it contains just these, then validate.
I tried to search on google but I could not find anything usefull.
Can anybody help?
Thank you !
Use the pattern '/^[A-Za-z0-9_-]*$/', if an empty string is also valid. Otherwise '/^[A-Za-z0-9_-]+$/'
So:
$yourString = "blahblah";
if (preg_match('/^[A-Za-z0-9_-]*$/', $yourString)) {
#your string is good
}
Also, note that you want to put a '-' last in the character class as part of the character class, that way it is read as a literal '-' and not the dash between two characters such as the hyphen between A-Z.
$data = 'abc123-_';
echo preg_match('/^[\w|\-]+$/', $data); //match and output 1
$data = 'abc..';
echo preg_match('/^[\w|\-]+$/', $data); //not match and output 0
You can use preg_replace($pattern, $replacement, $subject):
if (preg_replace('/[A-Za-z0-9\-\_]/', '', $string)) {
echo "Detect non valid character inside the string";
}
The idea is to remove any valid chars, if the result is NOT empty do the code.

PHP preg_match for only numbers and letters, no special characters

I don't want preg_match_all ... because the form field only allows for numbers and letters... just wondering what the right syntax is...
Nothing fancy ... just need to know the right syntax for a preg_match statement that looks for only numbers and letters. Something like
preg_match('/^([^.]+)\.([^.]+)\.com$/', $unit)
But that doesn't look for numbers too....
If you just want to ensure a string contains only alphanumeric characters. A-Z, a-z, 0-9 you don't need to use regular expressions.
Use ctype_alnum()
Example from the documentation:
<?php
$strings = array('AbCd1zyZ9', 'foo!#$bar');
foreach ($strings as $testcase) {
if (ctype_alnum($testcase)) {
echo "The string $testcase consists of all letters or digits.\n";
} else {
echo "The string $testcase does not consist of all letters or digits.\n";
}
}
?>
The above example will output:
The string AbCd1zyZ9 consists of all letters or digits.
The string foo!#$bar does not consist of all letters or digits.
if(preg_match("/[A-Za-z0-9]+/", $content) == TRUE){
} else {
}
If you want to match more than 1, then you'll need to, however, provide us with some code and we can help better.
although, in the meantime:
preg_match("/([a-zA-Z0-9])/", $formContent, $result);
print_r($result);
:)

How preg_match works exactly?

I've wrote a simple function to check if the string I send "should be" valid or not.
// this works without problems
function validate_email ($value) {
return preg_match ("/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[#][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/", $value);
}
// this doesn't work
function validate_string ($value) {
return preg_match ("([^<>?=/\]+)", $value);
}
the first function works well, if I send an email to validate_email I'm used to retain valid it return me 1 or 0 if not.
validate_string should do the same with strings of every kind but without ? = < > / \. If I check the function it return me 1 in anycase, why?
validate_string ("tonino"); // return 1 ok
validate_string ("ton\ino\"); // return 1 why?
validate_string ("ton?asd=3"); // return 1 why?
the ^ char inside ([^<>?=/]+) should mean not the chars after (or not?)
You aren't matching the beginning (^) and end ($) of the string. So "ton?asd=3" matches because the pattern matches t (and the rest of the string is irrelevant).
There are several errors in your code. Besides that "ton\ino\" is not a valid string and [^<>?=/\]+ is not a valid regular expression, you have probably some logical misunderstanding.
Your regular expression [^<>?=/\\]+ (here corrected) will match if there is at least one character that is not <, >, ?, =, / and \. So if there is at least one such character, preg_match returns 1. ton\ino" and ton?asd=3 do both contain at least one such character (the match is in both cases ton).
A fix for this is to either use assertions for the start and end of the string (^ and $) to only allow legal characters for the whole string:
^[^<>?=/\\]+$
Or to use a positive character class [<>?=/\\]+ to match the illegal characters and negate the returned expression of preg_match:
function validate_string ($value) {
return !preg_match("([<>?=/\\\\]+)", $value);
}
But it would be certainly better to use a whitelist instead of a blacklist.
\ is a meta character, you need to escape it. So it would be
return preg_match ("([^<>?=/\\\\]+)", $value);
function validate_string ($value) {
return !preg_match('#[<>?=/\\\\]#', $value);
}

Categories