Home > Backend Development > PHP Tutorial > 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

藏色散人
Release: 2023-04-10 13:50:01
Original
2519 people have browsed it

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:

<?php
$string = &#39;abcde$ddfd @abcd )der]&#39;;
echo &#39;原始字符串 : &#39;.$string.&#39;<br>&#39;;
$newstr = preg_replace("/[^A-Za-z0-9 ]/", &#39;&#39;, $string);
echo &#39;新字符串 : &#39;.$newstr."<br>";
Copy after login

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

About preg_replaceThe syntax is briefly introduced here:

mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template