Your proposed "validation" method is very ineffective .
Understand that what time texts have accents and certain characters and not another time , so you've made preg_match
forces to have all characters specified, this should not be an obligation, after all no text is the same as the other one, for example, your regex REQUIRES to have ¿
, but your text does not have it, then obviously it will fail. >
Regex is not just copy and put, regex is not like programming, in programming sometimes you write a bad code that works, in regex if you do not understand the basics will probably all fail even, I'll say it's rare people that really understand regex, here on the site has several answers with problems with regex, yes people who respond using regex and give problematic solutions.
I do not want to criticize these people, but rather explain that regex is something complicated, it's not like writing a hello world
if you skip the basics of this will give problem, your intention is probably validate only if you have one of these characters, but you used the following expressions:
-
^
Home the beginning of the string
-
+
Search to the next expression (in case your next is $
)
-
$
Home the end of the string or the position just before the end of the string break
Then in ^[a-zà -ÿ0-9\-+, .\#*()\/]+$
, ^
and +$
are forcing your string to be composed entirely of: a-zà -ÿ0-9\-+, .#*()\/
, that is to say everything, except that it has many problems in that expression, I'm honestly trying to compromise the reason why you did this: à -ÿ
, it just does not work, it looks like you copied that regex from somewhere, but that place was unicode mixed with latin1 that ended up converting the accents and failed everything.
I'm going to repeat, either use regex, learn the basics, regex is something that will rarely work ber in the base of Ctrl + C.
Well, let's face it, you probably want to validate a-z
and accented letters, you also want to validate a series of characters and punctuation, so let's separate everything that is needed:
- Required characters:
<
, >
, «
, »
, '
, &
, *
, #
, ?
!
, .
, +
, \
, -
, (
, )
and º
(anything is just add more), it would be something like \
- A to Z from 0 to 9, would be something like
/
- Spaces, TABs, and line breaks would be
[<>«»\'&*\#?!.+\-()º\/]
- Accents
[a-z0-9]
(requires modifier \s
)
This is the basics of your characters, now just merge everything into something like (note that I added the modifier \p{L}
which is for unicode, u
is for case-insentive):
'^[<>«»\'&*\#?!.+\-()º\/a-z0-9\p{L}\s]+$#ui'
It should look like this:
<?php
$data = 'OPEL ASTRA G 1.4 v1999
Lado esquerdo condutor(frente esq).
Elevador Original e Completo.
- Inclui Motor e restante.
- Elevador de 2º mão
Encontra-se a funcionar.
*Não nos responsabilizamos por defeitos futuros no artigo.';
var_dump( preg_match('#^[<>«»\'&*\#?!.+\-()º\/a-z0-9\p{L}\s]+$#iu', $data) );