Make the remaining characters lowercase.Limit the resulting string to eight characters.Strip all the characters from a string except the alphabet characters.(Again, I leave that output as "an exercise for the reader.") Bonus: A complete PHP string-cleaning functionīefore I go, I thought it might help to share the PHP function I wrote recently that led to this blog post.įor my purposes, I needed a PHP function to do the following things: (I'll leave that output to you.) Strip all characters but letters, numbers, and whitespaceįinally, if you want to strip all characters from your string other than letters, numbers, and whitespace, this regular expression will do the trick:
![php regex for number and spaces php regex for number and spaces](https://sgp1.digitaloceanspaces.com/ffh-space-01/9to5answer/uploads/post/avatar/74352/template_how-to-match-multiple-words-in-regex20220525-2610854-u5i1fl.jpg)
#Php regex for number and spaces code
Next, if we want to allow numbers as well as letters, we can modify our regular expression and preg_replace code to look like this: Strip all characters but letters and numbers from a PHP string
![php regex for number and spaces php regex for number and spaces](https://www.qreativbox.com/wp-content/uploads/2016/03/1-945x551.png)
If you put this line of code to work in a small PHP script, like this:Īnd then run that script, you’ll get the following output:Īs you can see, all the other characters have been stripped from the input string, leaving only letters in the resulting string. Here’s a quick PHP preg_replace example that takes a given input string, and strips all the characters from the string other than letters (the lowercase letters "a-z", and the uppercase letters "A-Z"): PHP string FAQ: How can I strip unwanted characters from a PHP string? (Also asked as, How can I delete unwanted characters from a PHP string?) Solution