PHP Regex String Allow Chinese Word, alphanumeric & few special characters - php

hi can help in below code to do validation in username text field.
Allow chinese word, alphanumeric and special characters "_" and "-" only.
Added:
I'm trying to create a validation for a username text field, allow chinese word, alphanumeric & "-" & "_" . I'm trying to figure out the regex as below, but it does not work as i expected. Anyone can hep.
if (preg_match("/[~`!##$%^&*()+={}\[\]|\\:;\"'<>,.?\/]/", "小明#ah meng"))
{
echo "invalid";
}
else
{
echo "valid";
}

The Han unification comprehends multiple code points from CJK. Since PCRE allows Unicode categories with the \p token, you can match most Chinese characters with \p{Han}.
Code:
<?php
$str = "小明ahmeng";
$regex = '/^[-_A-Za-z0-9\p{Han}]+$/u'; // notice spaces are not included
if (preg_match( $regex, $str)) {
echo "valid";
} else {
echo "invalid";
}
?>
DEMO
Also, don't forget to set the /u modifier when you're working with UTF-8 encoded strings.

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 Validation for alpha numeric characters

I have the following code for validating user input for alpha numeric characters
if (!preg_match("/^[A-Za-z][A-Za-z0-9.]*(?:_[A-Za-z0-9]+)*$/", $username)){
echo "invalid username";
}
It checks, whether the input have other characters than (A-Z) (0-9) alpha numeric. Additionally it allows to accept (_) underscore also, but it should be placed in between the strings only as my_name, but it will not accept _myname or myname_.
My question is how can I add a dot(.) character in my above code as same as constrained in underscore as Eg accept (my.name) or (myn.ame) etc but not to accept (.myname) (myname.)
I think this pattern should work for you
$string = 'my_nam.e';
$pattern = '/^[a-z]+([a-z0-9._]*)?[a-z0-9]+$/i';
if ( ! preg_match($pattern, $string))
{
echo 'invalid';
}

Regex to match a string that may contain Chinese characters

I'm trying to write a regular expression which could match a string that possibly includes Chinese characters. Examples:
hahdj5454_fd.fgg"
example.com/list.php?keyword=关键字
example.com/list.php?keyword=php
I am using this expression:
$matchStr = '/^[a-z 0-9~%.:_\-\/[^x7f-xff]+$/i';
$str = "http://example.com/list.php?keyword=关键字";
if ( ! preg_match($matchStr, $str)){
exit('WRONG');
}else{
echo "RIGHT";
}
It matches plain English strings like that dasdsdsfds or http://example.com/list.php, but it doesn't match strings containing Chinese characters. How can I resolve this?
Assuming you want to extend the set of letters that this regex matches from ASCII to all Unicode letters, then you can use
$matchStr = '#^[\pL 0-9~%.:_/-]+$#u';
I've removed the [^x7f-xff part which didn't make any sense (in your regex, it would have matched an opening bracket, a caret, and some ASCII characters that were already covered by the a-z and 0-9 parts of that character class).
This works:
$str = "http://mysite/list.php?keyword=关键字";
if (preg_match('/[\p{Han}]/simu', $str)) {
echo "Contains Chinese Characters";
}else{
exit('WRONG'); // Doesn't contains Chinese Characters
}

PHP preg_match pattern issue

I am trying to make this pattern work correctly and I just can't make it work it out. Basically, i want to validate a string that can only accept letter, numbers and the following characters: #!?_;:-,.
Here is the source code I have so far:
<?php
$test_string = '1234bcd#!?_;:-,.';
echo preg_match( "/^[a-z0-9#!?_;:,.]{4,12}$/i", $test_string );
?>
You're trying to print an integer, which won't achieve what you're trying to do. Try this:
if( preg_match( "/^[a-z0-9#!?_;:,.]{4,12}$/i", $test_string )) {
echo 'valid string!';
} else {
echo 'invalid!';
}
Note that your regex deems strings to be valid if they are:
Between 4 and 12 characters long
Consist of only alpabetic characters, numbers, and the other characters you've included.
Also note that your input string is supposed to be invalid, not only because it is too long, but because it contains a dash, which is not supported by your regex:
1234bcd#!?_;:-,.
^
No match
To include it in your regex, place it at the end of your character class in the regex:
preg_match( "/^[a-z0-9#!?_;:,.-]{4,12}$/i", $test_string )
^
some of the characters are reserved by regexp such as . ? etc.
use them with backslashes
if( preg_match( "/^[a-z0-9#!\?_;:,\.\-]{4,12}$/i", $test_string )) {
...
}
This pattern works :
$test_string = '1234bcd#!?_;:-,.';
if(preg_match('/^[a-z0-9#!\?_;:,\.\-]{4,12}$/i', $test_string))
{
echo 'Valid';
}
But in this case 1234bcd#!?_;:-,., it won't because the input string length is 16 (not between 4 and 12 characters long).
By the way, always escape meta-characters. You will find the complete list here.

Categories