Handling danish special characters [duplicate] - php

This question already has answers here:
how to replace special characters with the ones they're based on in PHP?
(8 answers)
Closed 8 years ago.
I am trying to parse a string, split it on what is not a letter or number
$parse_query_arguments = preg_split("/[^a-z0-9]+/i", 'København');
and construct a mysql query.
Even if I skip the preg_split and try to enter the string directly it breaks it into 2 different strings, 'K' and 'benhavn'.
How can I deal with these issues?

If you're using literal characters like a-z then it won't match accented ones. You might want to use the various character classes available to do more generic matching:
/[[:alpha:][:digit]]/
The [:alpha:] set is much broader in scope than a-z. Remember character matching is done based on character code, and a-z in order take, literally, characters between a and z by index. Characters like ø lie outside this range even if they'd fall between that alphabetically.
Computers work in ASCII-abetical (UNICODEical?) order.

This might help explain what is going on in your regex... Regex and Unicode.
You could try something like \p{L} as explained in this question

Related

Is it safe to mass replace legacy ASP tags with <?= ?> [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 4 years ago.
I have this RegEx:
('.+')
It has to match character literals like in C. For example, if I have 'a' b 'a' it should match the a's and the ''s around them.
However, it also matches the b also (it should not), probably because it is, strictly speaking, also between ''s.
Here is a screenshot of how it goes wrong (I use this for syntax highlighting):
I'm fairly new to regular expressions. How can I tell the regex not to match this?
It is being greedy and matching the first apostrophe and the last one and everything in between.
This should match anything that isn't an apostrophe.
('[^']+')
Another alternative is to try non-greedy matches.
('.+?')
Have you tried a non-greedy version, e.g. ('.+?')?
There are usually two modes of matching (or two sets of quantifiers), maximal (greedy) and minimal (non-greedy). The first will result in the longest possible match, the latter in the shortest. You can read about it (although in perl context) in the Perl Cookbook (Section 6.15).
Try:
('[^']+')
The ^ means include every character except the ones in the square brackets. This way, it won't match 'a' b 'a' because there's a ' in between, so instead it'll give both instances of 'a'
You need to escape the qutoes:
\'[^\']+\'
Edit: Hmm, we'll I suppose this answer depends on what lang/system you're using.

Strong passwords validation laravel [duplicate]

This question already has answers here:
Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters
(42 answers)
Closed 2 years ago.
So before any one shuts this down, I am referencing an answer and asking a question here.
So the question I have is I want a strong password validation for laravel, something that includes 10 character, numbers, upper lower case, so on and so forth.
I found it: https://stackoverflow.com/a/31549892/1270259
The problem is, this regex looks off:
/^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[\d\X])(?=.*[!$#%]).*$/
And since I am not good with regex I thought I would ask how can I fix it such that it validates:
Must be 10 characters long
Must contain upper and lower case
Must contain at least one number
Must contain at least one special character.
I feel like they were close in the answer. When this is run against github actions the error that comes back is preg_match(): Compilation failed: escape sequence is invalid in character class at offset 46.
Any thoughts as too how I can make this work for Laravel 7 to match the above constraints?
Use the principle of contrast:
^
(?=[^a-z]*[a-z]) # ensure one lower case letter
(?=[^A-Z]*[A-Z]) # ensure one upper case letter
(?=\D*\d) # ensure a digit
(?=[^!#?]*[!#?]) # special chars
.{10,} # at least 10 characters long
$
You can extend the special char section, of course.
See a demo on regex101.com.

Regex for name with space in HTML5 [duplicate]

This question already has an answer here:
html5 pattern for first and last name
(1 answer)
Closed 5 years ago.
I tried below pattern for Name field in HTML5, but everytime I am getting error :- "Please match the requested format"
Apart from above pattern, I also gave different pattern like :-
a) pattern="/^[A-Za-z\s]+$/"
b) pattern="/^[A-Za-z]\s[A-Za-z]+$/"
All the three pattern are not working. What I want is simple Firstname Lastname like "Harry Potter".
Please advice.
Thanks in advance.
You need to remove leading and trailing slashes from your pattern because in JavaScript they're indicate that string is actually a regular expression, but in html attribute it is already known to be a regular expression.
Pattern itself will depend on what kind of names you want to accept, your expression will not accept non-latin names, but there is a lot of people with such names. Basically if you want to check for existence of at least 2 words (since name can contain more then 2 words). For example you can use this pattern="\D\S+(\s+\D\S+)+" that will check for existence for at least 2 words separated by whitespace and each word should not start with a digit.

Need to switch from ereg() to preg_match() [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 9 years ago.
I need to know what this line of code does, tried to figure it out because i have to build it with preg_match() but I didn't understand it completely:
ereg("([0-9]{1,2}).([0-9]{1,2}).([0-9]{4})", $date)
I know it checks a date, but i don't know in which way.
thanks for some help
Let's break this down:
([0-9]{1,2})
This looks for numbers zero through nine (- indicates a range when used in brackets []) and there can be 1 or two of them.
.
This looks for any single character
([0-9]{1,2})
This looks for numbers zero through nine and there can be 1 or two of them (again)
.
This looks for any single character (again)
([0-9]{4})
This looks for numbers zero through nine and there must be four of them in a row
So it is looking for a date in any of the following formats:
04 18 1973
04-18-1973
04/18/1973
04.18.1973
More will fit that pattern so it isn't a very good regex for what it is supposed to validate against. There are lots of sample regex patterns for matting dates in this format so if you google it you'll have a PCRE in no time.
It's a relatively simple regular expression (regex). If you're going to be working with regex, then I suggest taking a bit of time to learn the syntax. A good starting place to learn is http://regular-expressions.info.
"Regular expressions" or "regex" is a pattern matching language used for searching through strings. There are a number of dialects, which are mostly fairly similar but have some differences. PHP started out with the ereg() family of functions using one particular dialect and then switched to the preg_xx() functions to use a slightly different regex dialect.
There are some differences in syntax between the two, which it is helpful to learn, but they're fairly minor. And in fact the good news for you is that the pattern here is pretty much identical between the two.
Beyond the patterns themselves, the only other major difference you need to know about is that patterns in preg_match() must have a pair of delimiting characters at either end of the pattern string. The most commonly used characters for this are slashes (/).
So in this case, all you need to do is swap ereg for preg_match, and add the slashes to either end of the pattern:
$result = preg_match("/([0-9]{1,2}).([0-9]{1,2}).([0-9]{4})/", $date);
^ ^
slash here and here
It would still help to get an understanding of what the pattern is doing, but for a quick win, that's probably all you need to do in this case. Other cases may be more complex, but most will be as simple as that.
Go read the regular-expressions.info site I linked earlier though; it will help you.
One thing I would add, however, is that the pattern given here is actually quite poorly written. It is intending to match a date string, but will match a lot of things that it probably didn't intend to.
You could fix it up by finding a better regex expression for matching dates, but it is quite possible that the code could be written without needing regex at all -- PHP has some perfectly good date handling functionality built into it. You'd need to consider the code around it and understand what it's doing, but it's perfectly possible that the whole thing could be replaced with something like this:
$dateObject = DateTime::CreateFromFormat($date, 'd.M.Y');
It looks like it would be pretty much agnostic in its matching.
You could interpret it either as mm.dd.yyyy or dd.mm.yyyy. I would consider modifying it if you were in fact trying to match/verify a date as 00.00.0000 would be a match but is an invalid data, outside of possible historic context.
Edit: I forget '.' in this case would match any character without escaping.
this do the same, i have only replace [0-9] by \d, and the dot (that match all) by \D (a non digit, but can replace it by \. or [.- ])
preg_match("~\d{2}\D\d{2}\D\d{4}~", $date)

php regular expression checking for name [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP regex to check a English name
I'm trying to write a regular expression that will check a name field that i have for a user.
I want it to check for
Firstname SPACE Middle initial SPACE Last name.
I know for singular letters for the middle intitial I'll need [a-z] but I'm not sure how to do the full words for first and last name and the spaces in between.
You might want to use str_word_count() to make it a bit easier for your self.
Also, to check for names you might want to add more letters than just a-z, you should take a look at the answer in this question.
Name may contain - symbol. /^[\-a-z]+\s[\-a-z]\s[\-a-z]+$/i
^[A-Za-z]+\s[A-Za-z]\s[A-Za-z]+$

Categories