1
I use Laravel and am trying to use the function preg_replace_callback()
to remove point, comma, hyphen, dash and bar from the CNPJ numbers entered in the system.
I used the function preg_replace_callback()
thus:
function parseTagsRecursive($input)
{
$regex = '#[^0-9]#';
return preg_replace_callback($regex, 'parseTagsRecursive', $input);
}
$cnpj = parseTagsRecursive($cnpj);
I’m getting back the following error:
preg_replace_callback(): Requires argument 2, 'parseTagsRecursive'
I used example 3 of documentation.
According to this example, I am implementing in the right way. Does anyone have any hint as to how I should proceed?
I also tried the preg_replace method as follows:
function deixarSomenteDigitos($input) {
return preg_replace('/[^0-9]/', '', $input);
}
$input['cgc_cl1'] = deixarSomenteDigitos($input['cgc_cl1']);
I used a website to generate a random cnpj for testing. It returned: 70,700,516/0001-24
By treating the field to save at the bank, the cnpj was saved the same way it entered.
Some points to consider: I am making these validations inside a Laravel Formrequest file, responsible for validating the data in my class.
Really man needs to be preg_replace_callback ?? has any specific reason to use it ?
– Joan Marcos
I used the preg_replace() method but, it seems that it was discontinued. I put it and nothing happened. The number has been saved with dots, commas and bars in the same way.
– keniaferreira
What has been discontinued is the function
ereg_replace
<- including the documentation recommends using thepreg_replace
in place– hkotsubo
Really, it’s not working. I used it like this:
– keniaferreira
$cnpj = preg_replace("/[ 0-9]/", "", $cnpj);
– keniaferreira
What was discontinued in the
preg_replace
was the support for modifiere
, which executed arbitrary code in the substitution (and to replace that specific mechanism, it is recommended to usepreg_replace_callback
). But the functionpreg_replace
in itself continues to exist normally.– hkotsubo
What should I change in this preg_replace code so it works?
– keniaferreira
@Kênia I am using
preg_replace
exactly as I indicated in my answer below and it is working. Maybe it is some problem elsewhere, because thepreg_replace
in itself is ok. I suggest you [Edit] the question and put the exact value of the CNPJ and the value you are getting with thepreg_replace
, who knows so we have a clue what might be your problem– hkotsubo
I imagine the problem is elsewhere then. I’ll try to find out here.
– keniaferreira
The specific part of
preg_replace
works: https://ideone.com/rGbKHt - maybe it’s time to save in the bank (and then I can’t help anymore, because I don’t know Laravel)– hkotsubo
Mano posts your controller or service of the Laravel to have vision of how you set saving pq is very strange.
– Joan Marcos
Solved! I was saving in Request. Request is only for data validation. It is not possible to change the writing of the data in it. I put the preg_replace directly in the Controller. Anyway, the topic served to clarify the ideas and I managed to understand the problem!
– keniaferreira