Temuulen Optimistic
Temuulen Optimistic

Reputation: 43

Php preg_replace numbers characters

$my_string = '88888805';
echo preg_replace("/(^.|.$)(*SKIP)(*F)|(.)/","*",$,my_string);

This shows the first and last number like thus 8******5 But how can i show this number like this 888888**. (The last 2 number is hidden)

Thank you!

From this: 8******5
To: 888888**

Upvotes: 0

Views: 33

Answers (2)

Abdullah R.
Abdullah R.

Reputation: 106

I'm not sure if you have worked on this Regex pattern to do something unique. However, I will provide you with a general one that should fit your question without using your current pattern.

$my_string = '88888805';
echo preg_replace("/([0-9]+)[0-9]{2}$/","$1**",$,my_string);

Explanation:

The ([0-9]+) will match all digits, this could be replaced with \d+, it's between brackets to be captured as we are going to use it in the results.

[0-9]{2} is going to match the last 2 digits, again, it can be replaced with \d{2}, it's outside the brackets because we don't want to include them in the result. the $ after that is to indicate the end of the test, it's optional anyways.

Results:

Input: 88888805
Output: 888888**

Upvotes: 2

joshpme
joshpme

Reputation: 115

echo preg_replace("/(.{2}$)(*SKIP)(*F)|(.)/","*",$my_string);

If it for a uni assignment, you'd probably want to do this. Basically says, don't match if its the last two characters, otherwise match.

Upvotes: 0

Related Questions