PHP (Preg_match)

Asked

Viewed 233 times

1

I’d like you to help define mine preg_match.

This will be for the creation of a textarea form $_POST['text'].

I want the pregmatch to just accept: Strokes, Dots, Bars, + and -, commas, spaces, numbers, letters, #and *.

preg_replace("", $data);
  • 1

    This seems a little vague. What is the format of the input data ? It does not matter the order of the characters to accept ?

  • this is for a textarea form... if the user inserts other characters than the ones I mentioned, the error.

  • 1

    Could you help? It’s for school :D

1 answer

2


If I understand you want the preg_match valide if it contains only the following types of characters:

  • traces
  • dots
  • bars
  • sign of +
  • sign of -
  • commas
  • spaces
  • numbers
  • letters
  • # (hash)
  • * (asterisk)

You can use the following :

^[a-z0-9\-+, .\#*\\/]+$

In the preg_match would be:

if (empty($_POST['text'])) {
    echo 'Não digitou nada';
} else {
    $text = $_POST['text'];

    if (preg_match('#^[a-z0-9\-+, .\#*\\/]+$#i', $text)) {
        echo 'Validou';
    } else {
        echo 'Não validou';
    }
}

Explaining the regex

This regex is very simple:

  • o the keys [...] and everything inside them will be accepted (will do the "match")
  • the sign of ^ indicates that you should start with exactly the following expression
  • the sign of $ indicates that it should end exactly with the previous expression
  • The sign of + between the ] and the $ (...]+$) indicates that it must have the format of what is inside [...] until it finds the next expression, as it has no expression, only the $ then ends there, (regardless of the amount of characters)

Extra explanations:

  • the a-z indicates that it may contain from A to Z
  • the \d indicates that it can contain from 0 to 9 (is equivalent to typing 0-9)
  • The \- indicates that it may contain hyphens (dash -), the bar is to escape and not mix with some expression shortcut
  • \\ is to accept inverted bars, use two bars so that the regex does not think you are trying to escape the next expression
  • The i in #...#i is to consider both uppercase and minuscule letters
^[a-z\d\-+, .\#*\\/]+$
^  ^                ^^
.  .                ..
.  .                .... Termina exatamente com a expressão anterior
.  .                .
.  .                .... busca até o "final" ou até a próxima expressão
.  .
.  ..................... tudo que estiver dentro de `[...]` será valido
.
........................ deve começar exatamente com a próxima expressão
  • Exact, the preg_match has to validate a POST with these requested characters.

  • You forgot to put the $text in the preg_match, but it would be right: preg_match('.... ', $test)) correct?

  • @Jonnhypi corrected, also had made some improvements in regex, copy again please.

  • Theoretically the circumflex indicates the beginning of the line(string). http://aurelionet/regex/guia/circumflex.html#2_3_1

  • @Marcosxavier the meaning is this same, if it refers to the next expression anyway, "must begin" = "from the beginning of the line" :]

  • @Guilherme Nascimento I understand and agree but for those who are starting, as described, may not understand this way.

  • @Marcosxavier the following expression is what comes after the ^.

  • 1

    Congratulations on the excellent answer.

  • Consider the following ER https://regexr.com/3o443. A beginner could understand that the other "Asd" would marry the ER. I’m not saying it’s wrong, but it can generate misinterpretation especially for beginners.

  • @Marcosxavier the following expression is just asd and not asdasdasdasd inside the regex, I’m talking about regex expressions and not the string.

Show 5 more comments

Browser other questions tagged

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