Last active
March 8, 2022 18:30
-
-
Save guimadaleno/9359247 to your computer and use it in GitHub Desktop.
How to easily remove characters using RegEx and 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 | |
# Removing symbols | |
$example1 = "String with letters, numbers 0123456789 and symbols !@#$%¨&*()_+"; | |
echo preg_replace("/[^a-zA-Z0-9\s]/", "", $example1); // String with letters numbers 0123456789 and symbols | |
# Remove symbols, including spaces | |
$example2 = "String with letters, numbers 0123456789 and symbols !@#$%¨&*()_+"; | |
echo preg_replace("/[^a-zA-Z0-9]/", "", $example2); // Stringwithlettersnumbers0123456789andsymbols | |
# Removing symbols and numbers | |
$example3 = "String with letters, numbers 0123456789 and symbols !@#$%¨&*()_+"; | |
echo preg_replace("/[^a-zA-Z\s]/", "", $example3); // String with letters numbers and symbols | |
# Removing letters and symbols | |
$example4 = "String with letters, numbers 0123456789 and symbols !@#$%¨&*()_+"; | |
echo preg_replace("/[^0-9\s]/", "", $example4); // 0123456789 | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment