
- 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
Printing Unwanted Characters with PHP âpreg_matchâ?
To print unwanted characters, use preg_match_all(). Let’s say the following is our string with some special characters −
$sentence= "M-y Name/i_s John+Doe";
We want the output to display only the special characters from the above string i.e.
-/_+
Example
The PHP code is as follows
<!DOCTYPE html> <html> <body> <?php $sentence= "M-y Name/i_s John+Doe"; echo "The original value is=",$sentence,"<br>"; if(preg_match_all('/[^a-zA-Z ]/', $sentence, $output) ){ echo "The unwanted characters are as follows= " . implode('', $output[0]); } ?> </body> </html>
Output
This will produce the following output
The original value is=M-y Name/i_s John+Doe The unwanted characters are as follows= -/_+
- Related Articles
- Match Expression in PHP 8
- How to match the regex meta characters in java as literal characters.
- How to match word characters using Java RegEx?
- PHP â Match regular expression using mb_ereg_match()
- Set printing double-sided documents with CSS
- How to match a range of characters using Java regex
- How to match date with MongoDB $match?
- Difference Between Transfer Printing and Digital Printing
- How to match a fixed set of characters using Java RegEx
- Easy Ways to Unsubscribe Unwanted Emails
- Avoiding unwanted directory while using zip
- Search for partial value match in an Array in PHP
- Methods of Printing
- Replacing specific characters from a matched string in PHP regular expression where we don't know the number of instances of the match?\n
- How to remove non-alphanumeric characters in PHP stirng?

Advertisements