Home > Backend Development > PHP Problem > A brief discussion on the usage of PHP regular replacement function preg_replace

A brief discussion on the usage of PHP regular replacement function preg_replace

醉折花枝作酒筹
Release: 2023-03-08 18:40:01
Original
3275 people have browsed it

This article uses several examples to learn the preg_replace function and understand its basic usage. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

PHP preg_replace() example

<?php
$str="jDTJNKLgfjn40000km4rfj185228554x";
echo $str;
echo &#39;<hr>&#39;;
echo preg_replace("/0/","",$str);//去掉0字符,此时相当于replace的功能, preg_replace("/0/","A",$str); 这样就是将0变成A的意思了
echo &#39;<hr>&#39;;
echo preg_replace("/[0-9]/","",$str);//去掉所有数字
echo &#39;<hr>&#39;;
echo preg_replace("/[a-z]/","",$str); //这样是去掉所有小写字母
echo &#39;<hr>&#39;;
echo preg_replace("/[A-Z]/","",$str); //这样是去掉所有大写字母
echo &#39;<hr>&#39;;
echo preg_replace("/[a-z,A-Z]/","",$str); //这样是去掉所有字母
echo &#39;<hr>&#39;;
$cmcc="gfjhrt1585dujk中国人dD中南海DfrtGHJ152YMet";
echo $cmcc;
echo &#39;<hr>&#39;;
echo preg_replace("/[a-z,A-Z,0-9]/","",$cmcc); //去掉所有字母和数字
?>
Copy after login

A brief discussion on the usage of PHP regular replacement function preg_replace

From this example we can intuitively see the usage of the preg_replace function. You can also see that the matching string must be added with / / (see the first parameter of the example). Without adding / /, there is no way to remove the numbers and letters we want to remove.

<?php
$str="jDT<JNKLgfjn40000km4rfj18>5228554x";
echo preg_replace("/<.*>/","",$str);
?>
Copy after login

This means removing the part starting with & and ending with >. The output result is: jDT5228554x.

Note: .* means any character, which means that no matter what the <> package is, it will be removed. . represents any character, * represents any number.

<?php
$str="sfrjjd<55555555>sc<6666>sd<>cd";
echo preg_replace("/<[0-9]*>/","",$str);//输出sfrjjdscsdcd
echo "<hr>";
echo preg_replace("/<[0-9]+>/","",$str);//输出sfrjjdscsd<>cd
?>
Copy after login

This example shows the difference between * and , * means repeating 0 or n times, and means at least once, that is, means must contain at least Only one number qualifies.
I believe you all know at this time why the output results of the same string using different characters are different.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of A brief discussion on the usage of PHP regular replacement function preg_replace. 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