identify a string having special charactor - php

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';
}

Related

PHP preg_match special characters only

I try to find a preg_match that filters strings that contain only special characters like -.
However, the string itself can contain special characters but it should not be special characters only.
Any ideas how to do that?
Result should return true if string only contains special characters. So something like
if(preg_match('//',$string)) echo $string; //I leave the pattern empty as this is the actual question.
You need to match the string for any alphanumeric character.
$one= "%^&";
$two = "asd%asd";
function notOnlySpecialChars($str) {
if (preg_match('/[A-Za-z0-9]/', $str)) { //Replaced _ with -
return true;
} else {
return false;
}
}
notOnlySpecialChars($one); //false
notOnlySpecialChars($two); //true

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

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

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.

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

Categories