Skip to content

Instantly share code, notes, and snippets.

@lucianobragaweb
Created July 17, 2023 01:37
Show Gist options
  • Save lucianobragaweb/b6cac9a547dcc6501b5cb3877dd1143a to your computer and use it in GitHub Desktop.
Save lucianobragaweb/b6cac9a547dcc6501b5cb3877dd1143a to your computer and use it in GitHub Desktop.
Deixar apenas os números de uma String PHP
<?php
$cpf = '123.158.124-22';
$cpf = preg_replace("/[^0-9]/", "", $cpf);
echo($cpf); // Resultado: 12315812422
<?php
function onlyNumbers($str) {
return preg_replace("/[^0-9]/", "", $str);
}
<?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