0
$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**
0

2 Answers 2

2

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**
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.