Home>Article>Backend Development> Remove all characters except az AZ 0-9 or null via PHP regex

Remove all characters except az AZ 0-9 or null via PHP regex

藏色散人
藏色散人 Original
2021-08-04 10:17:10 2396browse

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:

Remove all characters except az AZ 0-9 or null via PHP regex

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_replacefunction and the mastery of regular expression matching rules.

Aboutpreg_replaceThe 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn