
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
Strip punctuation with PHP
The ‘preg_replace’ function can be used to match the characters in the string and remove the unnecessary characters.
To keep letters and numbers −
Example
<?php $s = "Hello, my name is Bobby !? I am 8 years !"; print_r( preg_replace('/[^a-z0-9]+/i', ' ', $s)); ?>
Output
This will produce the following output −
Hello my name is Bobby I am 8 years
To keep letters only −
Example
<?php $s = "Hello, my name is Bobby !? I am 8 years !"; print_r( preg_replace('/[^a-z]+/i', ' ', $s)); ?>
Output
This will produce the following output −
Hello my name is Bobby I am years
To keep letters, numbers and underscore
Example
<?php $s = "Hello, my name is Bobby !? I am 8 years !"; print_r(preg_replace('/[^w]+/', ' ', $s)); ?>
Output
This will produce the following output −
Hello my name is Bobby I am 8 years
- Related Articles
- Check if string begins with punctuation in JavaScript
- How to strip all spaces out of a string in PHP?
- CSS speak-punctuation property
- CSS hanging-punctuation Property
- Strip quotes with JavaScript to convert into JSON object?
- MySQL query not matching due to punctuation?
- Set whether a punctuation character may be placed outside the line box with CSS
- A colourless liquid sample was tested with pH paper strip. The colour of the strip changed to reddish pink. The sample could be :
- Are punctuation marks a guide to good communication?
- Python program to Sort Strings by Punctuation count
- Solid sodium bicarbonate was placed on a strip of pH paper. The colour of the strip:
- MySQL strip time component from datetime?
- Where do the manufacturers use a magnetic strip in the refrigerator? Why is this magnetic strip used?
- JavaScript - Find if string is a palindrome (Check for punctuation)
- Check if a character is a punctuation mark in Arduino

Advertisements