Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing last x amount of numbers

Tags:

I have a PHP variable that looks a bit like this:

$id = "01922312";

I need to replace the last two or three numbers with another character. How can I go about doing this?

EDIT Sorry for the confusion, basically I have the variable above, and after I'm done processing it I'd like for it to look something like this:

$new = "01922xxx";
like image 792
esqew Avatar asked Dec 12 '10 00:12

esqew


People also ask

How to remove part of string PHP?

The substr() and strpos() function is used to remove portion of string after certain character.

How to replace a letter in a string JavaScript?

You can use the JavaScript replace() method to replace the occurrence of any character in a string. However, the replace() will only replace the first occurrence of the specified character. To replace all the occurrence you can use the global ( g ) modifier.

How to replace all numbers in selected cells with X in Excel?

Press the F5 key to run the code. Then you can see all numbers in selected cells are replaced with x as below screenshot shown. Related articles: How to find and replace all blank cells with certain number or text in Excel?

How to use the replace function in Excel?

The REPLACE function in Excel allows you to swap one or several characters in a text string with another character or a set of characters. As you see, the Excel REPLACE function has 4 arguments, all of which are required. Old_text - the original text (or a reference to a cell with the original text) in which you want to replace some characters.

How do I replace a character in a cell reference?

Click on cell A5 in the worksheet to enter that cell reference for the Old_text argument; Click on the Start_num line; Type the number 1 - starts the replacement from the first character on the left. Click on the Num_chars line; Type the number 3 on this line - the first three characters will be replaced;

When do last digits change to zeros when you type long numbers?

If the number that you type contains more than 15 digits, any digits past the fifteenth digit are changed to zero. Format the number as text to work around this problem. MET150 Last digits are changed to zeros when you type long numbers in cells of Excel - Office | Microsoft Docs Skip to main content This browser is no longer supported.


1 Answers

Try this:

$new = substr($id, 0, -3) . 'xxx';

Result:

01922xxx
like image 194
Mark Byers Avatar answered Oct 10 '22 13:10

Mark Byers