Home  >  Article  >  Backend Development  >  How to use regular rules to exclude characters in a string in php

How to use regular rules to exclude characters in a string in php

青灯夜游
青灯夜游Original
2022-12-15 15:30:295577browse

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);" .

How to use regular rules to exclude characters in a string in php

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(&#39;content-type:text/html;charset=utf-8&#39;);  
$str =&#39;0我是123456一段测试的字789符串0&#39;;
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:

How to use regular rules to exclude characters in a string in php

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

  • ##$subject for matching String

  • $matches All matching results (array)

preg_match_all() will match all results, if you want to match successfully once To stop matching later, you need to use the preg_match() function.

Example:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);  
$str =&#39;0我是123456一段测试的字789符串0&#39;;
echo "原字符串:".$str."<br><br>";

$result = &#39;&#39;;
preg_match_all(&#39;/\d+/&#39;,$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:

How to use regular rules to exclude characters in a string in php

will find that the result array

$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;

How to use regular rules to exclude characters in a string in php## Recommended learning: "

PHP Video Tutorial

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

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