Some fun with regular expression

Show only numbers in a string

 
<?php 
// strip out everything except for numbers
echo preg_replace('/[^0-9]/i', '', $string);
?>

Show only letters in a string

 
<?php 
// strip out everything except for letters
echo preg_replace('/[^a-z]/i', '', $string);
?>

Show only letters and numbers in a string

 
<?php 
// strip out everything except numbers and letters
echo preg_replace('/[^a-z0-9]/i', '', $string);
?>

Comments

comments

Leave a Reply