Hongmu Notes
Home Language Notes PHP regular replacement preg
Language Notes PHP PHP collection

PHP regular replacement preg

PHP regular replacement preg
<?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); //去掉所有字母和数字
?>
经过以上的例子,相信大家知道,[ ] 和里面的,有什么作用了。也可以看到,匹配的字符串必须加 / /(看例子的第一个参数)

<?php
$str="acsdcs<55555555>sc<6666>sdcd";
echo preg_replace("/<.*>/","",$str);

//这个是表示去除以<开头,以>结尾的那部份,输出结果是:acsdcssdcd
注意:上面的 .* 是表示任何字符,也就是说不管<>包住的是什么都去掉其中. 表示任意字符, * 表示任意个数

现在我们来改动一下,如果不想是任何个数呢?
$str="acsdcs<55555555>sc<6666>sdcd";
echo preg_replace("/<.{4}>/","",$str); //此时输出:acsdcs<55555555>scsdcd因为{4}指定了条件:<>内为4个字符的才满足条件,所以<55555555>不符合条件,没有被替换。

注意:这时我们又学到了一个知识点{数字} 表示指定前面的个数,*就表示是任意个(0--无限个)

表示重复次数的除了 *, {指定次数} 表示,还有很多表达形式:

 

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

上面的例子只要是为了 表达 * 与+的区别 ,* 表示重复0数或n 次,而+ 表示1次以上,即一例中<[0-9]+ >表示<>里面至少要有一个数字才符合条件。

相信这时大家知道,为什么上例中用* 和用+输出的结果不同了吧

再来:

<?php
$str="acsdcs<55555555>sc<6666>sd<>cd";
echo preg_replace("/<[0-9]?>/","",$str);
//输出acsdcs<55555555>sc<6666>sdcd
?>

看[0-9]? 这里的?表示要是0次或1 次,超过1次又不符合条件了。
总结一下,上面我们学会了 * + ? 和大括号{}表示重复次数的方法。

----------------------------------------------------------------------------------------------------

    $s=preg_replace("/(.*?[月票|求|更].*?)/i","",$s);
    preg_match_all('/href=\"([0-9]+)\.shtm">(.+?)<\/a>/i',$s,$arr_dstorycate);    
    print_r($arr_dstorycate);

----------------------------------------------------------------------------------------------------

   preg_match_all("/<img.*?src=[\\\'| \\\"](.*?(?:[\.gif|\.jpg]))[\\\'|\\\"].*?[\/]?>/i",$content,$arr_dstorycate);            
   print_r($arr_dstorycate); 

Supplement:

.$pattern is an array, and $replace is also an array, so the corresponding elements in it are replaced.

php

preg_replace has five parameters, three of which are required parameters

Preg_replace(mixed $pattern, mixed $replacement, mixed $subject [,int $limit = -1 [,int $count ]]);

$pattern can be a string, string array, or preg

$replace is the string or array of strings used for replacement

$subject target string or target string array

$limit The maximum number of substitutions for each pattern on each $subject. The default is -1 (unlimited times)

$count returns the number of times the target string has been replaced

There are several points to pay attention to in repalce and pattern.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232
PHP capture website source code and json parsing

PHP capture website source code and json parsing Language Notes PHP PHP collection

Recently, I wanted to collect the names of some resource websites, so I found a fun one when I collected it. The opposite website was loaded by ajax technology, so I found the api, so I just wrote the code directly to put it in the warehouse. The code is backed up here and will be used in the future. & lt; ? php $date = file_get_contents(' Website address ' ); $date = json_deco…
👁 136
PHP special character escaping and restoration

PHP special character escaping and restoration Language Notes PHP PHP collection PHP and mysql

Escape character is a special character constant. Escape characters are backslashed " & quot; At the beginning, followed by one or more characters. The escaped character has a specific meaning, which is different from the original meaning of the character, so it is called "escaped" character. The use of escape characters 1: turn ordinary characters into special purposes, such as back key and enter key. 2. Used to convert a character with special meaning back to its original meaning. 3. Before data is written into the database, escape characters (function …
👁 181

Recommended reading

Resource website typecho001 template

Resource website typecho001 template Program Notes Typecho

typecho001 template template please do not modify the folder name of this template. The folder name is: typecho001 1.4 Fix the js output problem on the article page 1.3 Fix the error prompt when the plug-in is not installed Optimize the list page code output 1.2 Fix the comment function and add a custom homepage title 1.1 Fix the comment reply asymmetry function Add a separate title setting function on the homepage Add the website favicon.ico icon Add one...
👁 199
Modification of typecho paging style

Modification of typecho paging style Program Notes Typecho

Sir, times have changed! Typecho is currently the most perfect solution, because Baidu can only see the code of fixed thinking. The actual generated HTML code is breathtakingly clean and fully customized, including adding classes to the li element, adding classes to the a element, adding classes to the previous page and next page, and removing the li tags that come with typecho to express more. I can even add some text to the content inside...
👁 517
Typecho article page comment style modification

Typecho article page comment style modification Program Notes Typecho

When you need to use typecho for template development, the default comment style is ugly. Therefore, comments need to be re-output or styled. The default comment template file path is: comments.php Mainly used related variables<?php $comments->gravatar('40', ''); ?> //Avatar has two parameters, size,...
👁 389
Typecho's API for adding, deleting, modifying and querying databases

Typecho's API for adding, deleting, modifying and querying databases Program Notes Typecho

Typecho database provides a very easy-to-use API, which is not much different from the native SQL writing method. At the same time, it also handles common SQL security issues, such as SQL injection. From a practical perspective, this article introduces common scenarios for typecho to operate databases and related API usage. Table creation and deletion During the development process of Typecho plug-in, you often need to create your own table. Typecho_… mentioned above
👁 552
PHP ob function record

PHP ob function record Language Notes PHP

Usage of the following three functions ob_get_contents(); ob_end_clean(); ob_start(); You can use these functions to buffer local files and execute local script code. Use ob_start() to save the output code into the buffer, and the page will not be displayed; then use ob_get_contents to get the data in the buffer. o…
👁 143
Summary of methods for calling popular comment articles and calling latest articles in Typecho

Summary of methods for calling popular comment articles and calling latest articles in Typecho Program Notes Typecho

Typecho articles call the Typecho program. When designing a theme, the sidebar sometimes needs to call popular articles or the latest articles. We can call it directly through the script at the specified location. In this article, we will organize this method of calling articles, which can be directly called and used in templates where needed in the future. In fact, designing a theme is just that. After the static template is completed, it is called directly. The latest article calls <?php $t…
👁 295