Regular expression, PHP

Asked

Viewed 522 times

2

What is the correct way to validate the string for the nomenclature below, using Regular expression?

/filmes/todos-filmes/notas-espectadores/?page=[SÓ NUMEROS]

3 answers

5


Basically this:

/filmes/todos-filmes/notas-espectadores/\?page=\d+

The backslash before the ? is because of ? alone be a special character.

The \d means "digit", and the + then means "one or more", ie it has to have one or more digits to be valid.


Applying to your case:

$padrao='~/filmes/todos-filmes/notas-espectadores/\?page=\d+~';

if preg_match($padrao, $endereco) {
   ...

See working on IDEONE.


When using the following code, we put ~ on the "tips" of string. They are not part of the expression, they are the delimiters, which separate the content from the flags when there is. It is very common to use / as delimiter, but as the text already has bars, the ~ is different enough not to confuse. The syntax is:

    delimitador expressão delimitador flags

Example:

    /\d+/i

In this case the expression is only \d+ and the flag is i. As / are only to separate. The first character used is to indicate which is the delimiter, and the next occurrence of it indicates end of expression.

  • those ~ is by someone’s influence? D

  • @Guilhermelautert updated the answer in more detail. You are a fan of ~ right. Suddenly you’re influencing the crowd, you’ll know :)

  • Yes, I always answer using it. And I recommend.

  • @Guilhermelautert I find it more readable in many situations, I agree with the preference. When it’s very simple thing I think the bar goes well, like /Você tem (\d+) mensagens/ but began to fill with symbol, the ~ seems much clearer to me.

0

Assuming that the expected behavior for an invalid parameter is 404. I recommend forgetting regular expression and doing so:

  $page = (int)$_GET['page'];
  if ($page==0) {
      header("HTTP/1.0 404 Not Found");
  }
  • 1

    I agree, it was a suggestion of an alternative solution, since the parameter "page" gives to understand that he wants to check if the number of the page provided is valid.

0

Exactly in this case it would be:

preg_match('/(?:page[\=]([0-9]+))/i', $_SERVER['REQUEST_URI'], $resultado);


if((isset($resultado[1]) == true) and ($resultado[1] != null)){

echo "Pagina existe! Vamos para lá?!";

} else {

echo "Essa página não existe cara! Elas só vão até 2!";

}

It would only take the numbers in the case, but it is mandatory that before the number comes to have "page=". On my site I do not use Wordpress or any kind of CMS, nor even make use of MVC everything on the site works on the basis of 'regular expressions'. It has a class that keeps an eye on everything that is typed in the URL or clicked and elaborates all the variables and communicates with other classes which possess such variables, some global due to their scope, determine how to assemble the page. I find regular expressions "fantastic". Save who invented this.

Browser other questions tagged

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