Reputation: 483
I am using this regex: preg_match('~[^A-Za-z0-9\+\-"]~', $incoming);
How can I add empty spaces here?
An example:
word1 word2
I mean the space between word1 and word2. I want to allow only one space. Not like word1 word2
(there are two spaces between these words).
But word1 word2 word3
shall be allowed.
Is that possible?
This is my current function:
$incoming = '"test test test"';
$forbidden_chars = preg_match('~[^A-Za-z0-9\+\-"\s]~', $incoming);
if( /*$string_length < 150 &&*/ !$forbidden_chars){
$valid_query = TRUE;
}
else{
$valid_query = FALSE;
}
$valid_query
should be true, but it is still false.
Why?
All examples:
"test"
test
+test
-test
test test
"test test"
shall be allowed, this shall also be possible test test test
this not: test test
(2 Spaces between the words)
Upvotes: 0
Views: 2533
Reputation: 403
you should simply handle this (removing duplicated space) within your method whatever input is, just simply prepare input before processing as following
$incoming= ereg_replace(' +', ' ', $incoming);
Upvotes: 0
Reputation: 40633
If you are trying to match the following:
Try this:
^ ?(\w+ ?)+$
EDIT 1:
Based on what I could figure out from your examples, here's what you need:
^ ?(?:[\w"+-]+ ?)+$
This will match 1 or more "words" that are single space separated. This allows optional leading and trailing single space.
Upvotes: 2