Optimization of php code (NF key generation)

Asked

Viewed 41 times

1

Dear colleagues, currently I am working on a project that carries out the sending of tax coupons, in it I arrived at a function that generates the key of the nfe, in it I receive the following data:

  • City code
  • Year and Month of issue
  • Cnpj of the Company
  • Model of the note (65 in the case)
  • Series
  • Note number (9 positions)
  • Type of issue
  • Note number with 8 positions

Function code:

public function GeraChaveNFe($CodCidade , $AnoMesEmissao, $CnpjEmpresa , $Modelo , $Serie, $NumeroNF, $TipoEmissao){
    $NF8 = "";
    $NF9 = "";
    $Chave = "";
    $Digito = "";

    $tam = strlen($NumeroNF);
    if($tam > 0){
        $NF9 = str_pad($NumeroNF, 9 - $tam, "0", STR_PAD_LEFT); 
        $NF8 = str_pad($NumeroNF, 8 - $tam, "0", STR_PAD_LEFT);
    }else{
        $NF9 = $this->right($NumeroNF, 9);
        $NF8 = $this->right($NumeroNF, 8);
    }

    $Chave = $CodCidade . $AnoMesEmissao . $CnpjEmpresa . $Modelo . $Serie . $NF9 . $TipoEmissao . $NF8;

    $Digito = $this->DigitoMod11($Chave);
    return $Chave . $Digito;
}

My doubt is in the following function:

function right($str, $length) {
    return substr($str, -$length);
}

Function Right does the same as String.Right VB and C#, when searching I didn’t find the same function in php... so I used substr.

The question is, is the substr really the equivalent of the right or is there a function that can replace this method?

1 answer

2


The question is, is the substr really the equivalent of the right or is there a function that can replace this method?

Yes, the function substr of php has the same functionality as the function String.Right of Visual Basic. In accordance with documentation of microsoft:

Returns a character string that contains a specified number of characters on the right side of a character string.

An example using the function Right using visual Basic:

Dim TestString As String = "Hello World!"    
Dim subString As String = Right(TestString, 6)
'Saida :  "World!"

Example taken from documentation.

Now, what a documentation of PHP tells about the function replace():

Returns the string part specified by the start and length parameter.

I will not go into detail about the function because that is not the focus of the question.

The same example from above using the language PHP with the function substr() :

$TestString = "Hello World!"    
$subString  = substr($TestString, 6)
//Saida :  "World!"

Completion

The two functions are equivalent and serve basically the same thing.

Note: I recommend reading the documentation of the function replace() because it can receive more parameters than those used here in the answer.

  • Thanks Jorge, this code fragment is the result of a migration of Vb technology to php, so as the deadline is short we ended up doing some "gambiarras" for the system to work, congratulations for the explanation

  • @Alvaroalves how good it was to you. You can use the function substr because it will satisfy what you did in the VB. I only recommend reading the documentation for more details.

Browser other questions tagged

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