validate a string consiting only of letters, numbers and optional spaces - php

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

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: str_word_count(åäöåäöåäöåäö) returns the integer value of 12

I am using special symbols such as å ä ö on my website which measures the lengths of different texts. Thing is, I have noticed that PHP counts the symbols "å" "ä" "ö" as 1 word each. So åäö counts as 3 words, and åäöåäöåäöåäöåäö counts as 15 words. Well this is clearly not correct and I cannot find an answer to this problem anywhere. I'd be thankful for a useful answer, thank you!
If there's a limited set of word characters that you need to take into account, just supply those into str_word_count with its third param (charlist):
$charlist = 'åäö';
echo str_word_count('åäöåäöåäöåäöåäö', 0, $charlist); // 1
Alternatively, you can write your own Unicode-ready str_word_count function. One possible approach is splitting the source string by non-word symbols, then counting the resulting array:
function mb_str_word_count($str) {
return preg_match_all('#[\p{L}\p{N}][\p{L}\p{N}\'-]*#u', $str);
}
Basically, this function counts all the substrings in the target string that start with either Letter or Number character, followed by any number (incl. zero) of Letters, Numbers, hyphens and single quote symbols (matching the description given in str_word_count() docs).
You can try adding
setlocale(LC_ALL, 'en_US.utf8')
before your call to str_word_count
or roll on your own with
substr_count(trim($str), ' ');
this work for me... hope its usefull.
USING str_word_count you need to use utf8_decode(utf8_encode)..
function cortar($str)
{
if (20>$count=str_word_count($str)) {
return $str;
}
else
{
$array = str_word_count($str,1,'.,-0123456789()+=?¿!"<>*ñÑáéíóúÁÉÍÓÚ#|/%$#¡');
$s='';
$c=0;
foreach ($array as $e) {
if (20>$c) {
if (19>$c) {
$s.=$e.' ';
}
else
{
$s.=$e;
}
}
$c+=1;
}
return utf8_decode(utf8_encode($s));
}
}
function returs 20 words
If it is a string without linebreaks, and words are separated by a whitespace, a simple workaround would be to trim() the string and then count the whitespaces.
$string = "Wörk has to be done.";
// 1 space is 2 words, 2 spaces are 3 words etc.
if(substr_count(trim($string), ' ') > 2)
{
// more than 3 words
// ...
}

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 regex ctype

I need a regex to see if the $input ONLY contained alphabetic characters or white spaces also need one to check if $numInput ONLY contained numeric characters or white spaces AND one combined so:
$alphabeticOnly = 'abcd adb';
$numericOnly = '1234 567';
$alphabeticNumeric = 'abcd 3232';
So in all of the above examples alphabetic, numeric, whitespace are allowed ONLY NO symbols.
How can I get those 3 diffrent regular expression?
This should help you
if (!preg_match('/^[\sa-zA-Z]+$/', $alphabeticOnly){
die('alpha match fail!');
}
if (!preg_match('/^[\s0-9]+$/', $numericOnly){
die('numeric match fail!');
}
if (!preg_match('/^[\sa-zA-Z0-9]+$/', $alphabeticNumeric){
die('alphanumeric match fail!');
}
This is pretty basic
/^[a-z\s]+$/i - letter and spaces
/^[\d\s]+$/ - number and spaces
/^[a-z\d\s]+$/i - letter, number and spaces
Just use them in preg_match()
In order to be unicode compatible, you should use:
/^[\pL\s]+$/ // Letters or spaces
/^[\pN\s]+$/ // Numbers or spaces
/^[\pL\pN\s]+$/ // Letters, numbers or spaces

How preg_match works exactly?

I've wrote a simple function to check if the string I send "should be" valid or not.
// this works without problems
function validate_email ($value) {
return preg_match ("/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[#][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/", $value);
}
// this doesn't work
function validate_string ($value) {
return preg_match ("([^<>?=/\]+)", $value);
}
the first function works well, if I send an email to validate_email I'm used to retain valid it return me 1 or 0 if not.
validate_string should do the same with strings of every kind but without ? = < > / \. If I check the function it return me 1 in anycase, why?
validate_string ("tonino"); // return 1 ok
validate_string ("ton\ino\"); // return 1 why?
validate_string ("ton?asd=3"); // return 1 why?
the ^ char inside ([^<>?=/]+) should mean not the chars after (or not?)
You aren't matching the beginning (^) and end ($) of the string. So "ton?asd=3" matches because the pattern matches t (and the rest of the string is irrelevant).
There are several errors in your code. Besides that "ton\ino\" is not a valid string and [^<>?=/\]+ is not a valid regular expression, you have probably some logical misunderstanding.
Your regular expression [^<>?=/\\]+ (here corrected) will match if there is at least one character that is not <, >, ?, =, / and \. So if there is at least one such character, preg_match returns 1. ton\ino" and ton?asd=3 do both contain at least one such character (the match is in both cases ton).
A fix for this is to either use assertions for the start and end of the string (^ and $) to only allow legal characters for the whole string:
^[^<>?=/\\]+$
Or to use a positive character class [<>?=/\\]+ to match the illegal characters and negate the returned expression of preg_match:
function validate_string ($value) {
return !preg_match("([<>?=/\\\\]+)", $value);
}
But it would be certainly better to use a whitelist instead of a blacklist.
\ is a meta character, you need to escape it. So it would be
return preg_match ("([^<>?=/\\\\]+)", $value);
function validate_string ($value) {
return !preg_match('#[<>?=/\\\\]#', $value);
}

Categories