Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace whole string with stars

Tags:

replace

php

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?

like image 356
Cyclone Avatar asked May 03 '12 19:05

Cyclone


People also ask

How do you replace a string with a star?

str = str. replaceAll("(? s).", "*");

How do you replace all characters in a string?

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.

What do the Replace All () do?

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.


1 Answers

$string = str_repeat('*', strlen($string));

Simply make a new string, consisting of all stars, with length equal to the original.

like image 58
Core Xii Avatar answered Oct 19 '22 08:10

Core Xii