Preg_match and percent sign [duplicate] - php

This question already has answers here:
allow parentheses and other symbols in regex
(5 answers)
Closed 7 years ago.
I have few questions regarding preg_match in php.
if(preg_match('#[^0-9 -&()+#._A-Za-z]#', $input)){
$errors .= 'Sorry, username can\'t contain those characters.<br>';
}
This is my preg_match. I am kinda new to these codes. I have red that its better to use # on the end and beginning than / for unknown reason xD
Anyone knows what is up with that?
My main problem is that this preg_match actually let strings with % (percent signs) through and it shouldn't. Why? and how to stop that?
Another question is this preg_match code good?
It works fine (except % part) but can it fail?
Thank you :)

this preg_match actually let strings with "%" (percent signs) through and it shouldn't. Why?
That is due to unescaped hyphen in the middle of your regex:
'#[^0-9 -&()+#._A-Za-z]#'
--------^
- is acting as range from space (32) to & (38) thus matching anything in between including % ( 37).
It should be used as:
'#[^-0-9 &()+#._A-Za-z]#'
Or
'#[^-\w &()+#.]#'
However without anchors this character class will match only one character. You should use:
'#^[^-\w &()+#.]+$#'

Related

PHP isolate character surrounded by special character [duplicate]

This question already has answers here:
Extract a single (unsigned) integer from a string
(23 answers)
Closed 6 years ago.
I have the following string:
$db_string = '/var/www/html/1_wlan_probes.db';
I want to isolate/strip the number character so that I only have the following left:
$db_string = '1';
So far I havn't found an simply solution since the number that needs to be found is random and could be any positive number. I have tried strstr, substr and custom functions but none produce what I am looking after, or I'm simply overlooking somehthing really simple.
Thanks in advance
You should use the preg_match() function:
$db_string = '/var/www/html/1_wlan_probes.db';
preg_match('/html\/(\d+)/', $db_string, $matches);
print_r($matches[1]); // 1
html\/(\d+) - capture all the numbers that come right after the html/
You can test it out Here. It does not matter how long the number is, you're using a regular expression to match all of them.

what does % sign do in preg_match_all? [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 7 years ago.
I am using this library with this line of text
#asdfasdf #日本語 スペース #漢字 #日本語 あ http://url file:///url「おくむら」最高ー!…
And it gives me right values
#asdfasdf #日本語
But in code there is % in regex
'%(\A#(\w|(\p{L}\p{M}?)|-)+\b)|((?<=\s)#(\w|(\p{L}\p{M}?)|-)+\b)|((?<=\[)#.+?(?=\]))%u'
What does this percent sign do?
In iOS it works without percent sign like this.
"(\\A#(\\w|(\\p{L}\\p{M}?)|-)+\\b)|((?<=\\s)#(\\w|(\\p{L}\\p{M}?)|-)+\\b)|((?<=\\[)#.+?(?=\\]))"
In php it gives me error: preg_match_all(): Unknown modifier '|'
What does this percent sign do?
Nothing, it is the delimiter to separate the regex from the additional options. They are required in the preg_* library, see http://php.net/manual/en/regexp.reference.delimiters.php.
Note that if you don't use the % in your regex, the ( ... ) will be used as the delimiter, leading to an error when you need to use them in your regex itself and you don't escape them.
Which is happening here in your case:
(\\A#(\\w|(\\p{L}\\p{M}?)|
^ Here the regex engine thinks you are closing the expression

PHP, how to check for uppercase/lowercase/numbers/special in regex [duplicate]

This question already has answers here:
Regex to validate password strength
(11 answers)
Closed 7 years ago.
I'm trying to create a class which validates parameters passed into a function, only primitive values though. I've so far done integer, boolean and float. However with my string function, I also want to be able to pass in a parameters about allowed character sets.
Like:
Allowed upper case
Allowed lower case
Allowed numbers
Allowed special characters
However, I can't figure out how to test this. I've tried doing it with regex, but my best trials aren't working too well.
Any nudge in the right direction would be a great help.
Please note, I'm using an old version for PHP - 5.1.2
Depends on what specials. Can try this and use with preg_match.
preg_match('/^[[:alnum:][:punct:]]+$/', $str)
Check out demo at regex101
[:alnum:] matches [a-zA-Z0-9]
[:punct:] matches [!"#$%&'()*+,\-./:;<=>?#[\\\]^_{|}~] and backtick.
+ one ore more from ^ start to $ end
See more posix classes
Try this:
if(!preg_match('/[.\s]/', $string))
{
// it contains any single character except dot and white space
// you can change not permitted list on your demand
}

PHP regular expression without delimiters doesn't work [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I convert ereg expressions to preg in PHP?
So I have some strings of this type:
choose_from_library_something
choose_from_library_something2
choose_from_library_something3
...
And I need to search for choose_from_library_*
here is my regular expression that doesn't work:
}elseif (preg_match('choose_from_library_.*',$form_name)) {
What I'm doing wrong?
You need to add delimiters to your regex:
preg_match('/^choose_from_library_.*/',$form_name)
EDIT: Added an anchor ^ to the beginning of the regex, to avoid matching don't_choose_from_library_, etc.
You may be better off using explode and count that off instead in your situation.
else if ( count ( explode("_" , $form_name) ) == 4 ) {
}
To answer your question, you need to have slashes around your pattern
/pattern/
also, asterisk means none or more, so even if you had no text, you it would still match it. The same would hold true for explode (but you can see if there was an empty string in the last element and return that as false).

Regex not working as expected [duplicate]

This question already has answers here:
What regex to use for this
(6 answers)
Closed 4 years ago.
I'm having troube modifying this regex. Right now it matches . or ? but I want to change it to match dot followed by a space. How do I do that?
'('/([.|?])/'
By the way, I need the grouping to stay.
What about this:
(\. |\?)
......
The easiest way would be:
'('/(\. )/'
or, if you want a space or a tab or a new-line:
'('/(\.\s)/'
Note that I only changed the part in the inner parenthesis as that part seems to be the focus of your question.
/\.\s/ should work for matching a dot followed by a space..
note: \s matches any whitespace

Categories