Replace the first 2 digits of a number from a $_POST

My phone system’s api returns the phone number as 61411111111 but in my database it is stored as 0411111111
So for me to be able to complete a search of the customer I need to replace just the first 2 digits (61) and replace with a 0.
What would be the best way to do this?

Is it always the first 2 numbers that you need to replace?

yes always the first 2

Ok so where are you using this value that needs a replacement? Are you filtering a database query using it? Or is it something else / somewhere else?

ok so api from the phone system returns “caller_id”: “61300000000”,
I need to remove the 61 and replace it with a 0 so I can then filter the database by the phone number 0300000000.

Created a small slice formatter for this (for NodeJS).
Extract it in your root folder and restart Wappler. Then you will see it under custom formatters in the Server Data Formatter:

extensions.zip (4.9 KB)

What it does is - you enter the start index, in your case 2:

And it will return everything after the 2nd character, like -> if your value is 61300000000 the result will be 300000000. You can add a 0 in front of it, so that it becomes 0300000000:

1 Like

Isn’t that the same as sub string? Had to do something similar yesterday

Works differently.
'abcdef'.substring(0, 2)
returns ab

'abcdef'.slice(2)
returns cdef

With slice you don’t need to know the exact string length, which is important in this exact use case.

Are you thinking of PHP?

substr('abcdef', 2)
returns cdef

@sitestreet you are correct :slight_smile: It’s just the UI in Wappler doesn’t allow this, as it expects a start index param, so you have to manually enter it:

{{('abcdef'.substr(2))}}

So my custom formatter is not really needed here :slight_smile:

1 Like

Worked perfectly, thanks for that. :slight_smile: