preg_match('/ d*$/', $nr_procedure) ? 'f' : ’t'; - what do ? php

Asked

Viewed 217 times

1

I have it (preg_match('/^\d*$/', $nr_procedimento) ? 'f' : 't';) in a code, but I have no idea what it does, more precisely the part of preg_match('/^\d*$/'. Does anyone know?

  • Vote today! Vote tomorrow! Vote always! Vote consciously! Your vote is very important to our community, contribute to us, and help make Stack Overflow in Portuguese (Sopt) bigger and bigger. You can learn more at: Vote early, vote often

2 answers

2


The function preg_match matches a regular expression in a string, returning 1 in match case, 0 if there was no match and false in case any error has occurred.

You pass two parameters to it, the first being a regular expression and the second the string to be checked.

In your case the regex is /^\d*$/, defining each part:

/ indicates the start of the regular expression

^ indicates that your pattern starts as follows

\d defines that they are numerical values

* defines that the previous element may occur 0 or n times

$ indicates that your string should end here

/ indicates the end of regular expression

You can see more about preg_match here.


In your complete example you have a ternary.

preg_match('/^\d*$/', $nr_procedimento) ? 'f' : 't';

In this case, if the contents of the variable $nr_procedimento der match na regex shown, the value f will be returned by the ternary, otherwise will be returned t.

  • 1

    It is worth noting that it is an instruction inline who will return f or t - the ugly way to FALSE or TRUE. I only commented here as an observation, because you gave the output of the function, and it will have something other than 0 or 1.

  • Huum legal, thank you for the explanation.

  • 1

    Well quoted @Papacharlie, I’m going to edit and add this. I ended up just explaining the preg_match for that is what he asked in the description of the question, but the title induces the rest.

1

Is a regular expression to find a line that starts and ends with numbers. The heaped symbols are known as meta characters, each has a function:

^ - means beginning of line.

\d - is an abbreviation for [0-9] or just numbers.

* - makes the greedy combination, if as many times as possible the defined pattern.

$ - means end of line

Browser other questions tagged

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