php preg_match validate string if alpha or have space - php

i need to validate a string that can only have letters(lowercase or uppercase) and may have space (may not have too) and a dash.
if(preg_match('/^[-a-zA-Z0-9]+$/', $myString)) {
//valid string
}

You could use this for a string contains only letter, space and dash, but at least one letter.
if(preg_match('/^(?=.*[a-z])[a-z -]+$/i', $myString)) {
echo 'valid';
}
Edit:
If the input must begin with any letter, then the regex could be simplified to:
if(preg_match('/^[a-z][a-z -]*$/i', $myString)) {
echo 'valid';
}

You can use this regex:
/^[a-zA-Z -]+$/
if(preg_match('/^[a-zA-Z -]+$/', $myString)) {
//valid string
}
EDIT If input must start with a letter then use:
/^[a-zA-Z][a-zA-Z -]*$/

Related

Compare username to regular expression with PHP

I've never used regular expressions before and did some research on how to allow my username field only alphanumeric characters, dashes, dots, and underscores. I have the following expression but it doesn't seem to be working.
$string = "Joe_Scotto";
if (!preg_match('[a-zA-Z0-9_-.]', $string)) {
echo "Does not match Regex";
} else {
echo "Matches";
}
I want the statement to return true if it is following the "guidelines" and false if the username contains something other than what I specified it should contain. Any help would be great. Thanks!
Try this
$string = "Joe_Scotto";
if (!preg_match('/^[A-Za-z0-9_.]+$/', $string)) {
echo "Does not match Regex";
} else {
echo "Matches";
}
You match only a single character. Try this:
$string = "Joe_Scotto";
if (!preg_match('/^[a-zA-Z0-9_.-]+$/', $string)) {
echo "Does not match Regex";
} else {
echo "Matches";
}
The + sign says: match 1 or more characters defined directly before the + (* is the same but matches 0 or more characters).
Also the separators '/' (or any other separator characters) are required.
And in character classes, it is better to place the - sign to the end, else it could be misinterpreted as range from _ to .
And add ^ at the beginning (this means: match from the beginning of the input) and $ to the end (this means: match to the end of the input). Else, also a part of the string would match.
You should use something like that http://www.phpliveregex.com/p/ern
$string = 'John_Buss';
if (preg_match('/[A-z0-9_\-.]+/', $string)) {
return true;
} else {
return false;
}
Make sure to add / delimiter character at the start and the end of your regex
Make sure to use \ escape character before -
Make sure to add + character quantifier

identify a string having special charactor

I have no. of strings and i want to identify those string which have special characters.
I am trying this to do above
if (!preg_match('/[^A-Za-z0-9]/', $url)) {
echo "special character";
}
And I have also tried:
if (ctype_alnum($url)) {
echo "special character";
}
The character i want to allow are a-z, A-Z, 0-9,_,-,/
And my string containing special character is like
torbjörn-hallber etc.
how can i do that ? please help.
Your first attempt with preg_match was good, just don't negate the return value.
if (preg_match('/[^A-Za-z0-9]/', $url)) {
echo 'special character';
}
You want to allow more characters including /, so I use ~ as a delimiter.
if (preg_match('~[^a-z0-9/_-]~i', $url)) {
echo 'special character';
}

PHP uppercase letter in String

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.

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]+$

How do I validate a first character must begin with A-Z

In PHP how do I validate user input like an example below.
Example valid input
$input ='abkc32453';
$input ='a32453';
$input ='dsjgjg';
Example invalid input
$input ='2sdf23';
$input ='2121adsasadf';
$input ='23142134';
if(ctype_alpha($input[0])){
//first character is alphabet
}
else {
//first character is invalid
}
if (preg_match('/^[a-z]/i', $input)) { /* "/i" means case independent */
...
}
or use [:alpha:] if you'd rather not use [a-z] (e.g. if you need to recognise accented characters).
You might try using a regular expression, with the preg_match() function :
if (preg_match('/^[a-zA-Z]/', $input)) {
// input is OK : starts with a letter
}
Basically, you search for :
beginning of string : ^
one letter : [a-zA-Z]
preg_match('%^[a-zA-Z].*%', $input, $matches);

Categories