Errors using PREG_MATCH

Asked

Viewed 121 times

-3

I created a form where you send information.

I think some character is missing in the PREG MATCH, because it does not accept the TEXT

TEXT

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.

PHP

if (!preg_match('#^[a-zà-ÿ0-9\-+, .\#*()\\/]+$#i', $ads_description)) {

What is missing in PREGMATCH?

  • 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?

  • 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...

  • I may have forgotten to add any character that the text has.

  • Check this sff~

  • 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.

  • So how do I get him to accept these characters as (a-Z, á 0-9, -+, .#*()\/)

  • I want to send a text where it accepts everything but "< > « » ' & #"

Show 2 more comments

1 answer

1

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) );
  • ok but use if(! preg match or if(pregmatch , with or without the !?

  • Caro @Snoopy12 does not exist pregmatch learn the basics with the php documentation: http://php.net/manual/en/function.preg-match.php - they show the basic use of all functions and the correct names, I edited the answer with an example, as I SAID AT THE END OF THE ANSWER: "I am editing the answer...", now that I’ve finished editing you can analyze.

  • gave error: Warning: preg_match(): Compilation failed: invalid UTF-8 string at offset 4 in /var/www/html/inc/Creator/class_create_ads.php on line 146

  • @Snoopy12 your document is saved as? UTF-8 or ANSI?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.