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

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);

Related

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.

php preg_match validate string if alpha or have space

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 -]*$/

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.

Extract a special hash from a string

I'm trying to extract the following pattern {#56DS1e5R9w7v} which is :
{
Hash
a-z, A-Z, 0-9 ( not necessarily an alphanumeric string )
}
Any ideas please?
Thank you
Try this pattern:
\{#([^}]*)\}
It should match all characters that are not }, and place the result in a captured group. You may want to change [^}]* to \w* or [A-Za-z0-9]* if that's problematic.
Example (also on ideone.com):
$str = "hello {#56DS1e5R9w7v} good people";
preg_match_all("/\{#([^}]*)\}/", $str, $matches);
Something like ({#[a-z0-9]+?}) ?
preg_match('/(\{#[a-z0-9]+?\})/i', $sString)
What about:
<?php
$code = '{#56DS1e5R9w7v}';
$matches = preg_match('/^\{#[a-zA-Z0-9]+\}$/', $code);
?>
This makes sure the string begins with a { the second char must be a #, the 3th char until } must be alpha numeric and it must end with a }.
Hope this helped!

preg_match special characters

How can I use preg_match to see if special characters [^'£$%^&*()}{#:'#~?><>,;#|\-=-_+-¬`] exist in a string?
[\W]+ will match any non-word character.
but to match only the characters from the question, use this:
$string="sadw$"
if(preg_match("/[\[^\'£$%^&*()}{#:\'#~?><>,;#\|\\\-=\-_+\-¬\`\]]/", $string)){
//this string contain atleast one of these [^'£$%^&*()}{#:'#~?><>,;#|\-=-_+-¬`] characters
}
Use preg_match. This function takes in a regular expression (pattern) and the subject string and returns 1 if match occurred, 0 if no match, or false if an error occurred.
$input = 'foo';
$pattern = '/[\'\/~`\!##\$%\^&\*\(\)_\-\+=\{\}\[\]\|;:"\<\>,\.\?\\\]/';
if (preg_match($pattern, $input)){
// one or more matches occurred, i.e. a special character exists in $input
}
You may also specify flags and offset for the Perform a Regular Expression Match function. See the documentation link above.
My function makes life easier.
function has_specchar($x,$excludes=array()){
if (is_array($excludes)&&!empty($excludes)) {
foreach ($excludes as $exclude) {
$x=str_replace($exclude,'',$x);
}
}
if (preg_match('/[^a-z0-9 ]+/i',$x)) {
return true;
}
return false;
}
The second parameter ($excludes) may be passed with values you wish to ignore.
Usage
$string = 'testing_123';
if (has_specchar($string)) {
// special characters found
}
$string = 'testing_123';
$excludes = array('_');
if (has_specchar($string,$excludes)) { } // false
For me, this works best:
$string = 'Test String';
$blacklistChars = '"%\'*;<>?^`{|}~/\\#=&';
$pattern = preg_quote($blacklistChars, '/');
if (preg_match('/[' . $pattern . ']/', $string)) {
// string contains one or more of the characters in var $blacklistChars
}
You can use preg_quote to escape charaters to use inside a regex expression:
preg_match('/' . preg_quote("[^'£$%^&*()}{#:'#~?><>,;#|\-=-_+-¬`]", '/') . '/', $string);
http://php.net/manual/en/function.preg-quote.php
This works well for all PHP versions. The resultant is a bool and needs to be used accordingly.
To check id the string contains characters you can use this:
preg_match( '/[a-zA-Z]/', $string );
To check if a string contains numbers you can use this.
preg_match( '/\d/', $string );
Now to check if a string contains special characters, this one should be used.
preg_match('/[^a-zA-Z\d]/', $string);
In case you want to match on special characters
preg_match('/[\'\/~`\!##\$%\^&\*\(\)_\-\+=\{\}\[\]\|;:"\<\>,\.\?\\\]/', $input)

Categories