Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created January 14, 2020 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/5965689f686ff8c4fa152b34652f3fc1 to your computer and use it in GitHub Desktop.
Save parzibyte/5965689f686ff8c4fa152b34652f3fc1 to your computer and use it in GitHub Desktop.
<?php
$texto = "Aunque aquí hay un guión bajo, no se debería remplazar: _
Ni este otro: luis_cabrera_benito
Pero lo siguiente, sí:
555_1213_555
000_123_512
Y esto, no:
123-456-789";
$patron = "/([0-9]+)_/";
# Aquí, $1 representa el primer grupo capturado, que sería el grupo de números
$sustitucion = "$1-";
$textoRemplazado = preg_replace($patron, $sustitucion, $texto);
printf("Antes:\n%s\nDespués:\n%s", $texto, $textoRemplazado);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment