- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
PHP program to remove non-alphanumeric characters from string
To remove non-alphanumeric characters from string, the code is as follows −
Example
<?php $my_str="Thisis!@sample*on&ly)#$"; $my_str = preg_replace( '/[^a-z0-9]/i', '', $my_str); echo "The non-alphanumeric characters removed gives the string as "; echo($my_str); ?>
Output
The non-alphanumeric characters removed gives the string as Thisissampleonly
The function ‘preg_replace’ is used to remove alphanumeric characters from the string. A regular expression is used to filter out the alphanumeric characters. The string is previously defined and the function ‘preg_replace’ is called on this and the reformed string is displayed on the console.
Example
<?php $my_str="This!#is^&*a)(sample*+_only"; $my_str = preg_replace( '/[W]/', '', $my_str); echo "The non-alphanumeric characters removed gives the string as "; echo($my_str); ?>
Output
The non-alphanumeric characters removed gives the string as Thisisasample_only
The only difference here is that a different regular expression is used. It means the same as the previous regular expression, but written in a different fashion.
- Related Articles
- How to remove non-alphanumeric characters in PHP stirng?
- How to remove all non-alphanumeric characters from a string in MySQL?
- JavaScript Remove non-duplicate characters from string
- C# Program to remove duplicate characters from String
- How to remove non-ASCII characters from strings
- Program to remove duplicate characters from a given string in Python
- Remove all non-alphabetical characters of a String in Java?
- JavaScript - Remove first n characters from string
- Python Program to Remove the nth Index Character from a Non-Empty String
- How to Remove Characters from a String in Arduino?
- C++ Program to remove Characters from a Numeric String Such That String Becomes Divisible by 8
- C++ program to remove characters from a numeric string to make it divisible by 8
- How to remove certain characters from a string in C++?
- How to remove specific characters from a string in Python?
- How to remove characters except digits from string in Python?

Advertisements