- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements