Created
July 17, 2023 01:37
-
-
Save lucianobragaweb/b6cac9a547dcc6501b5cb3877dd1143a to your computer and use it in GitHub Desktop.
Deixar apenas os números de uma String PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$cpf = '123.158.124-22'; | |
$cpf = preg_replace("/[^0-9]/", "", $cpf); | |
echo($cpf); // Resultado: 12315812422 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function onlyNumbers($str) { | |
return preg_replace("/[^0-9]/", "", $str); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo onlyNumbers('minha string com numeros 123.158.124-22'); // Resultado: 12315812422 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment