I need a solution to replace whole string with stars in PHP, for example there are strings like:
test
test123
test1234
And depends on string length, it will replace string with the stars like:
test
has 4 characters in length so it will be replaced with 4 stars ****
.
test123
has 7 characters in length so it will be replaced with 7 stars *******
. And so on...
Is there any good solution for that?
str = str. replaceAll("(? s).", "*");
To replace all occurrences of a substring in a string by a new one, you can use the replace() or replaceAll() method: replace() : turn the substring into a regular expression and use the g flag.
replaceAll() The replaceAll() method returns a new string with all matches of a pattern replaced by a replacement . The pattern can be a string or a RegExp , and the replacement can be a string or a function to be called for each match. The original string is left unchanged.
$string = str_repeat('*', strlen($string));
Simply make a new string, consisting of all stars, with length equal to the original.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With