I am trying to do some server side validation in PHP. I tried hard but I found still no solution. I am trying to allow only Polish characters in the input.
For this I have used:
preg_match('/^[\x{0104}-\x{017c}]*$/u',$titles)
This doesn't work however.
Anyone has any idea how to write it properly?
To match Polish letters only, you just need a character class:
[a-pr-uwy-zA-PR-UWY-ZąćęłńóśźżĄĆĘŁŃÓŚŹŻ]
Use as
preg_match('/^[A-PR-UWY-ZĄĆĘŁŃÓŚŹŻ]*$/iu',$titles)
Note that there is no Q, V and X in Polish, but since they can be met in some words (taxi), you may want to allow these letters as well. Then, use '/^[A-ZĄĆĘŁŃÓŚŹŻ]*$/iu' regex.
IDEONE demo
if (preg_match('/^[A-PR-UWY-ZĄĆĘŁŃÓŚŹŻ]*$/iu', "spółka")) {
echo "The whole string contains only Polish letters";
}
I'm new to PHP coding and I made a mistake in the code.
I have like 400 occurrences of a method that I want to change but I don't know how.
I've heard about regular expressions, but I can't apply to this case because I dont know so much of RegExp.
I have this in my code, for example
<?php echo $lang['extension_not_allowed'] ?>
I want to change all the results with $lang for $this->lang(''), for example
<?php echo $this->lang('extension_not_allowed') ?>
There's any way to do it using Ctrl+Shift+H (Global Replace) in NetBeans?
Thanks.
You can use the following regex with replacement:
(<\?php.*?)\$lang\[([^]]*)]
And the replacement:
$1\$this->\$lang($2)
Or remove the (<\?php.*?) part if the $lang may appear on lines without <?php:
\$lang\[([^]]*)]
and replace with "\$this->\$lang($1)".
See demo
A couple of notes:
(<\?php.*?) - matches and captures the text <?php and 0 or more characters other than a newline, as few as possible (with .*?)
\$lang\[ - matches $lang[ literally (note that special regex characters must be escaped)
([^]]*) - matches and captures 0 or more characters other than a ] (we are using a character class [...])
] - a literal ].
I have a question. How should look a regex for the following words:
+23456745678 or +29845281058, (with comma)
I tried but no result:
if (!preg_match('#^\+[0-9]{11}$#',$string)) {
return ("Msisdn $string is incorrect!");
break;
}
Always I get this error.
Please help me. Thx in advance
The pattern should be:
'/\+[0-9]{11},?/'
The ^ in the beginning and $ at the end stand for beginning and end of line, which I am not sure if you really want there (my guess is not).
Test it here: http://www.phpliveregex.com/p/78v
Another option is to use \d for digit
'/\+\d{11},?/'
I am stuck trying to create a regex that will allow for letters, numbers, and the following chars: _ - ! ? . ,
Here is what I have so far:
/^[-\'a-zA-Z0-9_!\?,.\s]+$/ //not escaping the ?
and this version too:
/^[-\'a-zA-Z0-9_!\?,.\s]+$/ //attempting to escape the ?
Neither of these seem to be able to match the following:
"Oh why, oh why is this regex not working! It's getting pretty frustrating? Frustrating - that is to say the least. Hey look, an underscore_ I wonder if it will match this time around?"
Can somebody point out what I am doing wrong? I must point out that my script takes the user input (the paragraph in quotes in this case) and strips all white space so actual input has no white space.
Thanks!
UPDATE:
Thanks to Lix's advice, this is what I have so far:
/^[-\'a-zA-Z0-9_!\?,\.\s]+$/
However, it's still not working??
UPDATE2
Ok, based on input this is what's happening.
User inputs string, then I run the string through following functions:
$comment = preg_replace('/\s+/', '',
htmlspecialchars(strip_tags(trim($user_comment_orig))));
So in the end, user input is just a long string of chars without any spaces. Then that string of chars is run using:
preg_match("#^[-_!?.,a-zA-Z0-9]+$#",$comment)
What could possibly be causing trouble here?
FINAL UPDATE:
Ended up using this regex:
"#[-'A-Z0-9_?!,.]+#i"
Thanks all! lol, ya'll are going to kill me once you find out where my mistake was!
Ok, so I had this piece of code:
if(!preg_match($pattern,$comment) || strlen($comment) < 2 || strlen($comment) > 60){
GEEZ!!! I never bothered to look at the strlen part of the code. Of course it was going to fail every time...I only allowed 60 chars!!!!
When in doubt, it's always safe to escape non alphanumeric characters in a class for matching, so the following is fine:
/^[\-\'a-zA-Z0-9\_\!\?\,\.\s]+$/
When run through a regular expression tester, this finds a match with your target just fine, so I would suggest you may have a problem elsewhere if that doesn't take care of everything.
I assume you're not including the quotes you used around the target when actually trying for a match? Since you didn't build double quote matching in...
Can somebody point out what I am doing wrong? I must point out that my script takes the user input (the paragraph in quotes in this case) and strips all white space so actual input has no white space.
in which case you don't need the \s if it's working correctly.
I got the following code to work as expected to (running php5):
<?php
$pattern = "#[-'A-Z0-9_?!,.\s]+#i";
$string = "Oh why, oh why is this regex not working! It's getting pretty frustrating? Frustrating - that is to say the least. Hey look, an underscore_ I wonder if it will match this time around?";
$results = array();
preg_match($pattern, $string, $results);
echo '<pre>';
print_r($results);
echo '</pre>';
?>
The output from print_r($results) was as following:
Array
(
[0] => Oh why, oh why is this regex not working! It's getting pretty frustrating? Frustrating - that is to say the least. Hey look, an underscore_ I wonder if it will match this time around?
)
Tested on http://writecodeonline.com/php/.
It's not necessary to escape most characters inside []. However, \s will not do what you want inside the expression. You have two options: either manually expand (/^[-\'a-zA-Z0-9_!?,. \t\n\r]+$/) or use alternation (/^(?:[-\'a-zA-Z0-9_!?,.]|\s)+$/).
Note that I left the \ before the ' because I'm assuming you're putting this in a PHP string and I wouldn't want to suggest a syntax error.
The only characters with a special meaning within a character class are:
the dash (since it can be used as a delimiter for ranges), except if it is used at the beginning (since in this case it is no part of any range),
the closing bracket,
the backslash.
In "pure regex parlance", your character class can be written as:
[-_!?.,a-zA-Z0-9\s]
Now, you need to escape whatever needs to be escaped according to your language and how strings are written. Given that this is PHP, you can take the above sample as is. Note that \s is interpreted in character classes as well, so this will match anything which is matched by \s outside of a character class.
While some manuals recommend using escapes for safety, knowing the general regex rules for character classes and applying them leads to shorter and easier to read results ;)
Ok, here again.
I'll promise to study deeply the regular expression soon :P
Language: PhP
Problem:
Match if some badword exist inside a string and do something.
The word must be not included inside a "greater word". I mean if i'll search for "rob" (sorry Rob, i'm not thinking you're a badword), the word "problem have to pass without check.
I'd googled around but found nothing good for me. So, I thought something like this:
If i match the word with after and before any character of the following:
.
,
;
:
!
?
(
)
+
-
[whitespace]
I can simulate a check against single word inside a string.
Finally the Questions:
There's a better way to do it?
If not, which will be the correct regexp to consider [all_that_char]word[all_that_char]?
Thanks in advance to anyone would help!
Maybe this is a very stupid question but today is one of that day when move our neurons causes an incredible headache :|
Look up \b (word boundary):
Matches at the position between a word
character (anything matched by \w) and
a non-word character (anything matched
by [^\w] or \W) as well as at the
start and/or end of the string if the
first and/or last characters in the
string are word characters.
(http://www.regular-expressions.info/reference.html)
So: \brob\b matches rob, but not problem.
You can use \b, see Whole word bounderies.