<blockquote><p>How to use PHP regular replacement preg_replace function: 1. Remove 0 characters, the code is [preg_replace("/0/","",$str)]; 2. Remove all numbers, the code is [preg_replace ("/[0-9]/","",$str)]. </p></blockquote>
<p><img src="https://img.php.cn/upload/article/202008/06/2020080614223025495.jpg" alt="How to use PHP regular replacement preg_replace function" ></p>
<p><strong>How to use PHP regular replacement preg_replace function: </strong></p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><?php
$str="as2223adfsf0s4df0sdfsdf";
echo preg_replace("/0/","",$str);//去掉0字符,此时相当于 replace的功能, preg_replace("/0/","A",$str); 这样就是将0变成A的意思了
echo preg_replace("/[0-9]/","",$str);//去掉所有数字
echo preg_replace("/[a-z]/","",$str); //这样是去掉所有小写字母
echo preg_replace("/[A-Z]/","",$str); //这样是去掉所有大写字母
echo preg_replace("/[a-z,A-Z]/","",$str); //这样是去掉所有字母
$str="as2223adfsAAf0s4df0s中国人dD中南海DDfsdf";
echo preg_replace("/[a-z,A-Z,0-9]/","",$str); //去掉所有字母和数字
?></pre><div class="contentsignin">Copy after login</div></div><p>After the above examples, I believe everyone knows,<code>[ ] </code> and what’s inside it. You can also see that the matching string must be added with <code>/ /</code> (see the first parameter of the example) </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><?php
$str="acsdcs<55555555>sc<6666>sdcd";
echo preg_replace("/<.*>/","",$str);</pre><div class="contentsignin">Copy after login</div></div><p> means that the string starting with <code><</code> is removed. For the part ending with <code>></code>, the output result is: acsdcssdcd<br/></p><p>Note: The .* above means any character, that is, regardless of the <> is to remove everything. . represents any character, * represents any number </p><p> Now let’s change it, what if we don’t want it to be any number? </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">$str="acsdcs<55555555>sc<6666>sdcd";</pre><div class="contentsignin">Copy after login</div></div><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">echo preg_replace("/<.{4}>/","",$str);</pre><div class="contentsignin">Copy after login</div></div><p>//Output at this time: acsdcs<55555555>scsdcd Because {4} specifies the condition: only 4 characters within the <> satisfy the condition, so <55555555> does not meet the condition, no be replaced. </p><p>Note: At this time we learned another knowledge point {number} means specifying the previous number, * means any number (0--unlimited) </p><p> means the number of repetitions In addition to *, {specified times}, there are many expressions: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><?php
$str="acsdcs<55555555>sc<6666>sd<>cd";
echo preg_replace("/<[0-9]*>/","",$str);
//输出acsdcscd
echo "<hr>";
echo preg_replace("/<[0-9]+>/","",$str);
//输入acsdcsscsd<>cd
?></pre><div class="contentsignin">Copy after login</div></div><p>The above example is just to express the difference between * and, * means repeating 0 times or n times, and means repeating 1 or more times, that is In one example, <[0-9] > means that there must be at least one number in <> to qualify. </p><p>I believe everyone knows at this time why the output results of using * and using in the above example are different</p><p>Come again: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><?php
$str="acsdcs<55555555>sc<6666>sd<>cd";
echo preg_replace("/<[0-9]?>/","",$str);
//输出acsdcs<55555555>sc<6666>sdcd
?></pre><div class="contentsignin">Copy after login</div></div><p>Look at [0-9]? Here ? means that if it is 0 or 1 times, it does not meet the conditions if it is more than 1 time. </p>
<p>To summarize, above we learned how * ? and braces {} represent the number of repetitions. </p>
<blockquote><p>Related learning recommendations: <a href="https://www.php.cn/course/list/29.html" target="_blank">php programming</a> (video)<br></p></blockquote>
The above is the detailed content of How to use PHP regular replacement preg_replace function. For more information, please follow other related articles on the PHP Chinese website!