Your proposed validation "method" is quite ineffective.
Understand what time texts have accents and certain characters and another time not, the way you did the preg_match
obliges to have all characters specified, being that this should not be obligatory, after all no text is equal to another, for example, your regex OBLIGES to have the ¿
, but your text does not have, so 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 will say is rare people who even understand of regex, here on the site have several answers with regex problems, yes people who respond using regex and give problematic solutions.
I’m not wanting 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 yes, your intention is probably only validate if you have one of these characters, but you used the following expressions:
^
Box the beginning of the string
+
Search to the next expression (in the case of your next one is $
)
$
Box the end of the string or position just before the end of the string break
Then in ^[a-zà -ÿ0-9\-+, .\#*()\\/]+$
, the ^
and +$
are obliging their string to be composed entirely of: a-zà -ÿ0-9\-+, .#*()\/
, I’m going to say it all, aside from having a lot of problems with that expression, I’m honestly trying to understand why you did it: à -ÿ
, this simply doesn’t work, it seems that you copied from somewhere this regex, but the such place was with problem of mixing Unicode with latin1 that ended up converting the accents and failed everything.
I’ll repeat, want to use regex, learn the basics, regex is one thing that will rarely work Ber at the base of Ctrl+C.
Well let’s get to the point, 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:
- Characters required:
<
, >
, «
, »
, '
, &
, *
, #
, ?
, !
, .
, +
, \
, -
, (
, )
, º
, \
and /
(anything just add more), it would be something like [<>«»\'&*\#?!.+\-()º\\/]
- From A to Z from 0 to 9, it would be something like
[a-z0-9]
- Spaces, Tabs and line breaking would be the
\s
- Accents
\p{L}
(requires the modifier u
)
Ready this is the basics of your characters, now just merge everything into something like (note that I added the modifier u
which is for Unicode, the i
is for case-insentive):
'^[<>«»\'&*\#?!.+\-()º\\/a-z0-9\p{L}\s]+$#ui'
Should stay 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) );
Dear Snoopy12 I can’t understand the problem of your question, I can’t even understand what you really want your preg_match to do, is it a validation? Could you edit the question to better explain what you expect this script to do?
– Guilherme Nascimento
what the preg match ai has is.. If the Text $ads_description, does not have this "'# [a-z -It is 0-9-+, .#()\/]+$#i'" of the error! The problem is that this "'# [a-zà -ã¿0-9-+, .#()\/]+$#i'" is missing something...
– Snoopy12
I may have forgotten to add any character that the text has.
– Snoopy12
Check this sff~
– Snoopy12
Well, I’m trying to help, but really this validation method that you propose is quite ineffective, aside from what time texts have accents and another time no, the way you did the preg_match requires having all characters specified, and that shouldn’t be obligation, after all, no text is the same as the other, for example, your regex
¿
, but your text does not have, I obviously will fail.– Guilherme Nascimento
So how do I get him to accept these characters as (a-Z, á 0-9, -+, .#*()\/)
– Snoopy12
I want to send a text where it accepts everything but "< > « » ' & #"
– Snoopy12