Skip to content

Instantly share code, notes, and snippets.

@guimadaleno
Last active March 8, 2022 18:30
Show Gist options
  • Save guimadaleno/9359247 to your computer and use it in GitHub Desktop.
Save guimadaleno/9359247 to your computer and use it in GitHub Desktop.
How to easily remove characters using RegEx and PHP
<?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