- Trending Categories
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
How to remove non-alphanumeric characters in PHP stirng?
We can remove non-alphanumeric characters from the string with preg_replace() function in PHP. The preg_replace() function is an inbuilt function in PHP which is used to perform a regular expression for search and replace the content.
syntax
preg_replace(pattern,replacement,subject,limit,count )
Let's discuss the parameters of the function underneath.
pattern
This parameter contains the pattern to search for.
replacement
It is a mandatory parameter. This parameter may contain a string or an array with strings to replace.
subject
The string or an array with strings to search and replace.
limit
The maximum possible replacements for each pattern in each subject string
count
This is an optional parameter, if specified then this variable will be filled with the number of replacements done.
Let's demonstrate with an example to remove non-alphanumeric characters from a string with the help of preg_replace() function.
Example
<?php $str = "tu!tor^ials$%!poi&nt"; echo preg_replace('/[^a-zA-Z0-9]/','', $str); ?>
Output
tutorialspoint
Explanation
In the above example, a string "$str" contains non-alphanumeric characters. We have used preg_replace() used to extract those characters from the strings to get the desired output.
- Related Articles
- PHP program to remove non-alphanumeric characters from string
- How to remove all non-alphanumeric characters from a string in MySQL?
- How to remove non-word characters in JavaScript?
- How to remove non-ASCII characters from strings
- JavaScript Remove non-duplicate characters from string
- How to write Python regular expression to check alphanumeric characters?
- Remove all non-alphabetical characters of a String in Java?
- What is ASCII Alphanumeric Characters in Computer Architecture?
- How to remove unique characters within strings in R?
- How to Remove Characters from a String in Arduino?
- Python - Check If All the Characters in a String Are Alphanumeric?
- 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?
- How to remove unconvertable characters to ASCII with SELECT in MySQL?
