search
HomeBackend DevelopmentPHP ProblemA brief discussion on the usage of PHP regular replacement function preg_replace

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); //去掉所有字母和数字
?>

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);
?>

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
?>

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools