Home > Article > Backend Development > How to use regular rules to exclude characters in a string in php
Two methods: 1. Use preg_replace() to perform regular expression search and replacement. You only need to replace the matching characters in the string with empty characters. The syntax "preg_replace(regular, " ", $str)". 2. Use preg_match_all() to search for all results matching the regular expression in the string. Each matching result will be placed in an array $array. The syntax is "preg_match_all(regular, $str, $array);" .
The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer
What is a regular expression Formula
Regular expression is a logical formula for string operations. It uses some predefined specific characters and combinations of these specific characters to form a "regular string. ", this "rule string" is used to express a filtering logic for strings.
Regular expressions construct patterns with specific rules, compare them with the input string information, and use them in specific functions to achieve operations such as string matching, search, replacement, and splitting.
php uses regular expressions to filter strings
In PHP, you can use regular expressions to filter strings, exclude (delete) or extract specified character.
Method 1. Use the preg_replace() function
The preg_replace() function can perform regular expression search and replacement and is a powerful string replacement processing function.
preg_replace($pattern, $replacement, $subject [, $limit = -1 [, &$count]])
means to search the part of subject that matches pattern and replace it with replacement; the optional parameter limit is used to set the maximum number of replacements (the default value is -1, which can be unlimited).
Example:
<?php header('content-type:text/html;charset=utf-8'); $str ='0我是123456一段测试的字789符串0'; echo "原字符串:".$str."<br>"; $result = preg_replace("/[^0-9]/", "", $str); echo "过滤后字符串:".$result; ?>
preg_replace("/[^0-9]/", "", $str)
It means to match characters other than numbers between 0~9
, and replace these characters with null characters (that is, delete these characters), then only numeric characters are left:
Method 2: Use the preg_match_all() function
The preg_match_all() function can search for all results in the string that can match the regular expression. It is a A function that performs global regular expression matching. Its syntax format is:
preg_match_all($pattern, $subject [, &$matches [, $flags = PREG_PATTERN_ORDER [, $offset = 0 ]]])
$pattern regular expression
<?php header('content-type:text/html;charset=utf-8'); $str ='0我是123456一段测试的字789符串0'; echo "原字符串:".$str."<br><br>"; $result = ''; preg_match_all('/\d+/',$str,$array); echo "过滤结果:"; var_dump($array); foreach($array as $arr){ foreach($arr as $value){ $result .= $value; } } echo "过滤后字符串:".$result; ?>
preg_match_all('/\d /',$str,$array) means that multiple searches in the string $str only contain A substring of numeric characters between
0~9, and each matching result is placed in an array
$array. We use
var_dump($array) to output this array and see:
$array is a two Dimensional array, the elements containing the matching results are the inner arrays. If you want to splice these elements into a string, you need to nest two levels of foreach loops:
foreach($array as $arr){ foreach($arr as $value){ $result .= $value; } } echo "过滤后字符串:".$result;
## Recommended learning: "
PHP Video TutorialThe above is the detailed content of How to use regular rules to exclude characters in a string in php. For more information, please follow other related articles on the PHP Chinese website!