PHP Regular Expression Exercise: Remove all characters from a string except a-z A-Z 0-9 or " "
PHP regular expression: Exercise-7 with Solution
Write a PHP script to remove all characters from a string except a-z A-Z 0-9 or " ".
Sample string: abcde$ddfd @abcd )der]
Pictorial Presentation:

Sample Solution:
PHP Code:
<?php
$string = 'abcde$ddfd @abcd )der]';
echo 'Old string : '.$string.'
';
$newstr = preg_replace("/[^A-Za-z0-9 ]/", '', $string);
echo 'New string : '.$newstr."\n";
?>
Sample Output:
Old string : abcde$ddfd @abcd )der] New string : abcdeddfd abcd der
Flowchart :

PHP Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a PHP script to extract text (within parenthesis) from a string.
Next: PHP Date Exercises Home.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join