Replace this 123456 with this 000000 in php [closed]

1

hi I have a little doubt I would like to replace a string by 0

$cadena = '1234567'
    
asked by Josbert Hernandez Riera 16.05.2017 в 17:37
source

3 answers

2

You can use the str_repeat function that repeats a string n times, in this case the number of characters that%% of%

has
$cadena = str_repeat( "0" , strlen($cadena) )

Demo

    
answered by 16.05.2017 в 17:40
2

You can use the preg_replace function with a regular expression that replaces each numeric character with a% % co:

$cadena = '1234567';
$cadena0 = preg_replace('/\d/', '0', $cadena);

Here you have a link from Ideone .

    
answered by 16.05.2017 в 17:47
1
$cadena = '1234567'
str_replace('1234567','0000000',$cadena);

Note: This way you can replace the substring '1234567' with '0000000' in any other string other than '1234567' as well (for example 'qqs1234567acv').

    
answered by 16.05.2017 в 17:48