Home>Article>Backend Development> Remove all characters except az AZ 0-9 or null via PHP regex
In "Delete non-numeric characters except commas and dots through PHP regular expressions" we will introduce to you how to use regular expressions to delete non-numeric characters except commas and dots. I believe you already have some understanding of the use of regular expressions, so today I will continue to bring you examples of using PHP regular expressions~
As the title states, in this article we will delete strings through regular expressions All characters except az AZ 0-9 or " ".
First give you an example string:abcde$ddfd @abcd )der]
You can first write a method locally to delete the string. All characters except az AZ 0-9 or " ".
The following is my method:
'; $newstr = preg_replace("/[^A-Za-z0-9 ]/", '', $string); echo '新字符串 : '.$newstr."
";
Let’s run the result directly, as shown in the figure below:
You can see This result is what our title requires!
Isn’t it so easy!
The use of regular expressions in PHP is nothing more than the use of thepreg_replace
function and the mastery of regular expression matching rules.
Aboutpreg_replace
The syntax is briefly introduced here:
mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
means that you can search for the part of the subject that matches the pattern and replace it with replacement.
Attachment:
Introduction to the concept of regular expressions:
Regular expression is a logical formula for string operations, which uses some specific characters defined in advance. and the combination of these specific characters to form a "rule string". This "rule string" is used to express a filtering logic for strings.
Finally, I would like to recommend "Regular Expression Manual" to everyone. I believe that as long as you read this tutorial carefully and make certain references when applying it, mastering regular expressions will not be a problem.
The above is the detailed content of Remove all characters except az AZ 0-9 or null via PHP regex. For more information, please follow other related articles on the PHP Chinese website!