preg_replace_callback() to remove characters that are not digits

Asked

Viewed 613 times

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&#039

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 ?

  • 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.

  • What has been discontinued is the function ereg_replace <- including the documentation recommends using the preg_replace in place

  • Really, it’s not working. I used it like this:

  • $cnpj = preg_replace("/[ 0-9]/", "", $cnpj);

  • What was discontinued in the preg_replace was the support for modifier e, which executed arbitrary code in the substitution (and to replace that specific mechanism, it is recommended to use preg_replace_callback). But the function preg_replace in itself continues to exist normally.

  • What should I change in this preg_replace code so it works?

  • @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 the preg_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 the preg_replace, who knows so we have a clue what might be your problem

  • I imagine the problem is elsewhere then. I’ll try to find out here.

  • 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)

  • Mano posts your controller or service of the Laravel to have vision of how you set saving pq is very strange.

  • 1

    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!

Show 7 more comments

1 answer

3


If you just want to remove everything that is not numbers, you can simply use preg_replace:

echo preg_replace('/[^0-9]/', '', '74.208.285/0001-97'); // 74208285000197

In this case, anything that is not a number ([^0-9]) is replaced by the empty string (''), that is, these characters are removed, and in the end only the numbers remain.

If you want, you can create a function for it:

function deixarSomenteDigitos($input) {
    return preg_replace('/[^0-9]/', '', $input);    
}

echo deixarSomenteDigitos('74.208.285/0001-97'); // 74208285000197

preg_replace_callback is useful when you need to manipulate the pouch in some non-trivial way (something that requires a function for such, as in the examples cited in documentation).

And example 3 of the documentation is even more complex, because it uses recursive regex (since it is manipulating a structure that can be recursive), and makes the substitutions recursively as well.

But this is not your case as you just need to detect everything that is not digit and remove, so it is not necessary a callback (and much less recursive).

  • I imagine the problem is another then. Because I did exactly like you:

  • Function stopSomenteDigitos($input) { Return preg_replace('/[ 0-9]/', '', $input); } $cnpj = stopSomenteDigitos($cnpj);

  • And it doesn’t work.

  • @Kenya I suggest you edit the question and put the exact value of the $cnpj that is using and the value that is getting as a result, who knows so we have a clue of what may be happening

Browser other questions tagged

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