Created
January 14, 2020 00:35
-
-
Save parzibyte/5965689f686ff8c4fa152b34652f3fc1 to your computer and use it in GitHub Desktop.
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 | |
$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