Get some alphabet characters and nothing else - php

I want to check if the input is Persian and it's at least 3 characters.
When I use below regex it works. It checks if the beginning of the word is Persian, but I want it to end with Persian, too, and to contain no English alphabet and no numbers.
/^[ئابپتثجچحخدذرزژسشصضطظعغفقکگلمنوهآی\s]{3,}/
For ensuring it ends with Persian I add $ after ] but I get this error:
Warning: preg_match(): Compilation failed: nothing to repeat at offset 77
/^[ئابپتثجچحخدذرزژسشصضطظعغفقکگلمنوهآی\s]${3,}/
Also, what is \s before the closing bracket ]? Is it for new line?

Use
/^[ئابپتثجچحخدذرزژسشصضطظعغفقکگلمنوهآی\s]{3,}$/u
$ is the end of line/string anchor and is not quantifiable (i.e. you cannot use a + or * or {1,} after it).
See demo
As for \s, it just matches whitespace from this set: [\r\n\t\f ].
EDIT: Using Rishida Unicode Converter, I re-wrote your expression using \x blocks:
/^[\x{626}-\x{628}\x{67E}\x{62A}-\x{62C}\x{686}\x{62D}\x{62E}\x{62F}-\x{632}\x{698}\x{633}-\x{63A}\x{641}\x{642}\x{6A9}\x{6AF}\x{644}-\x{648}\x{622}\x{6CC}]{3,}$/u
It does not allow spaces and looks nicer than ^[ئابپتثجچحخدذرزژسشصضطظعغفقکگلمنوهآی]{3,}$ that should also work (BTW, in MS Word it will look prettier :)).

To match arabic letters you can use:
^[\x{600}-\x{6FF}]{3,}$
if (preg_match('/^[\x{600}-\x{6FF}]{3,}$/u', $value)) {
# match
} else {
# no match
}

Related

How can I check a string's pattern with given conditions (php/regex)

This is what I'm trying to do,
$line = "dsfsdf";
if (!preg_match('/^(?=.{1,30}$)[a-zA-Z0-9\-\_]*$^/', $line))
{
echo 'No Match found';
}
else
{
echo 'Match found';
}
The requirement is below,
it can have characters
it can have numbers
As special character, it can have only hyphen (-) and underscore (_) characters in it
I'm not so good at regex part. Can someone guide me how to achieve it with a simple explanation?
You must remove ^ (start of string anchor) at the end. Also, you may replace [a-zA-Z0-9_] with \w, as without any modifiers, they are equal.
The (?=.{1,30}$) lookahead makes the regex engine only match strings with 1 to 30 characters. You may remove the lookahead and just apply the limiting quantifier to your character class.
You may use
'/^[\w-]{1,30}$/'
If you prefer a more verbose way use
'/^[a-zA-Z0-9_-]{1,30}$/'
See the PHP demo.
Both mean:
^ - start of string
[\w-]{1,30} - 1 to 30 letters/digits/underscores/- symbols
$ - end of string. NOTE that to match at the very end of the string, you need to use a D modifier, or replace $ with \z anchor (i.e. use '/^[\w-]{1,30}$/D' or '/^[\w-]{1,30}\z/' then).

check the value entered by the user with regular expression in php

in my program php, I want the user doesn't enter any caracters except the alphabets
like that : "dgdh", "sgfdgdfg" but he doesn't enter the numbers or anything else like "7657" or "gfd(-" or "fd54"
I tested this function but it doesn't cover all cases :
preg_match("#[^0-9]#",$_POST['chaine'])
how can I achieve that, thank you in advance
The simplest can be
preg_match('/^[a-z]+$/i', $_POST['chaine'])
the i modifier is for case-insensitive. The + is so that at least one alphabet is entered. You can change it to * if you want to allow empty string. The anchor ^ and $ enforce that the whole string is nothing but the alphabets. (they represent the beginning of the string and the end of the string, respectively).
If you want to allow whitespace, you can use:
Whitespace only at the beginning or end of string:
preg_match('/^\s*[a-z]+\s*$/i', $_POST['chaine'])
Any where:
preg_match('/^[a-z][\sa-z]*$/i', $_POST['chaine']) // at least one alphabet
Only the space character is allowed but not other whitespace:
preg_match('/^[a-z][ a-z]*$/i', $_POST['chaine'])
Two things. Firstly, you match non-digit characters. That is obviously not the same as letter characters. So you could simply use [a-zA-Z] or [a-z] and the case-insensitive modifier instead.
Secondly you only try to find one of those characters. You don't assert that the whole string is composed of these. So use this instead:
preg_match("#^[a-z]*$#i",$_POST['chaine'])
Only match letters (no whitespace):
preg_match("#^[a-zA-Z]+$#",$_POST['chaine'])
Explanation:
^ # matches the start of the line
[a-zA-Z] # matches any letter (upper or lowercase)
+ # means the previous pattern must match at least once
$ # matches the end of the line
With whitespace:
preg_match("#^[a-zA-Z ]+$#",$_POST['chaine'])

Regular expression any character but a white space

I'm creating a password validator which takes any character but whitespaces and with at least 6 characters.
After searching the best I came up is this is this example:
What is the regular expression for matching that contains no white space in between text?
It disallows any spaces inbetween but does allow starting and ending with a space. I want to disallow any space in the string passed.
I tried this but it doesn't work:
if (preg_match("/^[^\s]+[\S+][^\s]{6}$/", $string)) {
return true;
} else {
return false;
}
Thanks.
Something like this:
/^\S{6,}\z/
Can be quoted like:
preg_match('/^\S{6,}\z/', $string)
All answers using $ are wrong (at least without any special flags). You should use \z instead of $ if you do not want to allow a line break at the end of the string.
$ matches end of string or before a line break at end of string (if no modifiers are used)
\z matches end of string (independent of multiline mode)
From http://www.pcre.org/pcre.txt:
^ start of subject
also after internal newline in multiline mode
\A start of subject
$ end of subject
also before newline at end of subject
also before internal newline in multiline mode
\Z end of subject
also before newline at end of subject
\z end of subject
The simplest expression:
^\S{6,}$
^ means the start of the string
\S matches any non-whitespace character
{6,} means 6 or more
$ means the end of the string
In PHP, that would look like
preg_match('/^\S{6,}$/', $string)
Edit:
>> preg_match('/^\S{6,}$/', "abcdef\n")
1
>> preg_match('/^\S{6,}\z/', "abcdef\n")
0
>> preg_match('/^\S{6,}$/D', "abcdef\n")
0
Qtax is right. Good call! Although if you're taking input from an HTML <input type="text"> you probably won't have any newlines in it.
I think you should be fine using the following, which would match any string longer than 1 character with no whitespace:
^[^\s]+$
You can see the test here: http://regexr.com?2ua2e.
Try this. This will match at least 6 non whitespace characters followed by any number of additional non whitespace characters.
^[^\s]{6}[^\s]*$
\S - Matches any non-white-space character. Equivalent to the Unicode character categories [^\f\n\r\t\v\x85\p{Z}]. If ECMAScript-compliant behavior is specified with the ECMAScript option, \S is equivalent to [^ \f\n\r\t\v].
The start of string you can do : ^[ \t]+, and for end : [ \t]+$ (tab and spaces)
ETA:
By the way, you regex [\S+], i think you're looking for : [\S]+

preg_match in php

I want to use preg_match() such that there should not be special characters such as ``##$%^&/ '` in a given string.
For example :
Coding : Outputs valid
: Outputs Invalid(String beginning with space)
Project management :Outputs valid (space between two words are valid)
'Design23' :Outputs valid
23Designing : outputs invalid
123 :Outputs invalid
I tried but could not reach to a valid answer.
Does a regex like this help?
^[a-zA-Z0-9]\w*$
It means:
^ = this pattern must start from the beginning of the string
[a-zA-Z0-9] = this char can be any letter (a-z and A-Z) or digit (0-9, also see \d)
\w = A word character. This includes letters, numbers and white-space (not new-lines by default)
* = Repeat thing 0 or more times
$ = this pattern must finish at the end of the string
To satisfy the condition I missed, try this
^[a-zA-Z0-9]*\w*[a-zA-Z]+\w*$
The extra stuff I added lets it have a digit for the first character, but it must always contain a letter because of the [a-zA-Z]+ since + means 1 or more.
Try
'/^[a-zA-Z][\w ]+$/'
If this is homework, you maybe should just learn regular expressions:
Regular expressions tutorial
Regular expressions reference
PCRE syntax reference for PHP

PHP Regular Expression [accept selected characters only]

I want to accept a list of character as input from the user and reject the rest. I can accept a formatted string or find if a character/string is missing.
But how I can accept only a set of character while reject all other characters. I would like to use preg_match to do this.
e.g. Allowable characters are: a..z, A..Z, -, ’ ‘
User must able to enter those character in any order. But they must not allowed to use other than those characters.
Use a negated character class: [^A-Za-z-\w]
This will only match if the user enters something OTHER than what is in that character class.
if (preg_match('/[^A-Za-z-\w]/', $input)) { /* invalid charcter entered */ }
[a-zA-Z-\w]
[] brackets are used to group characters and behave like a single character. so you can also do stuff like [...]+ and so on
also a-z, A-Z, 0-9 define ranges so you don't have to write the whole alphabet
You can use the following regular expression: ^[a-zA-Z -]+$.
The ^ matches the beginning of the string, which prevents it from matching the middle of the string 123abc. The $ similarly matches the end of the string, preventing it from matching the middle of abc123.
The brackets match every character inside of them; a-z means every character between a and z. To match the - character itself, put it at the end. ([19-] matches a 1, a 9, or a -; [1-9] matches every character between 1 and 9, and does not match -).
The + tells it to match one or more of the thing before it. You can replace the + with a *, which means 0 or more, if you also want to match an empty string.
For more information, see here.
You would be looking at a negated ^ character class [] that stipulates your allowed characters, then test for matches.
$pattern = '/[^A-Za-z\- ]/';
if (preg_match($pattern, $string_of_input)){
//return a fail
}
//Matt beat me too it...

Categories