Home > headlines > Complete knowledge points about str_replace in php

Complete knowledge points about str_replace in php

PHP中文网
Release: 2017-07-23 15:41:58
Original
3017 people have browsed it

一php str_replace preg

The data that we need to process with programs is not always designed in advance with database thinking, or it cannot be stored using the structure of the database. of.
For example, template engine parsing templates, spam sensitive information filtering, etc.
Generally in this case, we use regular expressions to match preg_match and replace preg_replace according to our rules.
But in general applications, they are nothing more than database CRUD, and there are very few opportunities to fiddle with regular expressions.
According to what was said before, there are two scenarios: statistical analysis, using matching; processing using replacement.

PHP preg_replace() regular replacement is different from Javascript regular replacement. PHP preg_replace() defaults to replacing all elements whose symbols match the conditions.

The regular expressions in most languages ​​are similar, but there are also subtle differences.

PHP Regular Expression

preg_replace (正则表达式, 替换成, 字符串, 最大替换次数【默认-1,无数次】, 替换次数)
Copy after login
#$ matches the end position of the input string. When the Multiline property of the RegExp object is set, $ also matches the position before "\n" or "\r" ##*##{n}n is a non-negative integer. For example, "o{2}" cannot match "Bob". "o" in "food", but can match two o's in "food". ##{n,}{n,m}?##(pattern)(?:pattern)(?=pattern)(?!pattern)(?<=pattern)##(?Reverse negative pre-check is similar to forward negative pre-check, but in the opposite direction. For example, "(?x|y Matches x or y. For example, "z|food" matches "z" or "food". "(z|f)ood" matches "zood" or "food". [xyz]Character collection. Matches any one of the characters contained. For example, "[abc]" would match the "a" in "plain".
Regular charactersRegular explanation
\Mark the next character as a special character , or a literal character, or a backreference, or an octal escape character. For example, "\n" matches the character "n". "\\n" matches a newline character. The sequence "\\" matches "\" and "\(" matches "(".
^ matches the beginning of the input string. If set The Multiline property of the RegExp object, ^ also matches the position after "\n" or "\r"
matches the preceding subexpression zero. times or multiple times. For example, zo* can match "z" and "zoo". * is equivalent to {0,}. Subexpression one or more times. For example, "zo+" can match "zo" but not "z". + is equivalent to {1,}. #? Matches the preceding subexpression zero or one time. For example, "do(es)?" can match "do" in "does" or "does". ? is equivalent to {0, 1}.
n is a non-negative integer that matches at least n. Times. For example, "o{2,}" cannot match "o" in "Bob", but it can match all o's in "foooood". "o{1,}" is equivalent to "o+". 0,}" is equivalent to "o*".
m and n are non-negative integers, where n<=m . Match at least n times and at most m times. For example, "o{1,3}" will match the first three o's in "fooooood". "o{0,1}" is equivalent to "o?". Note that there cannot be a space between the comma and the two numbers.
When this character is followed by any other limiter (*, +,?, {n}, {n,}, {n,m}), the matching mode is non-greedy. The non-greedy mode matches as little of the searched string as possible, while the default greedy mode matches as much as possible. The string to search for. For example, for the string "oooo", "o?" will match a single "o", while "o+" will match all "o" dots. Matches any single character except "\n". To match any character including "\n", use a pattern like "[\s\S]".
Matches pattern and obtains this match. The obtained match can be obtained from the generated Matches collection, using the SubMatches collection in VBScript and $0 in JScript. …$9 properties. To match parentheses characters, use "\(" or "\)".
Matches pattern but does not obtain the matching result, which means that this is a non-acquisition match and is not stored for later use. This is useful when combining parts of a pattern using the or character "(|)". For example, "industr(?:y|ies)" is a simpler expression than "industry|industries".
Forward positive pre-check, match the search string at the beginning of any string matching pattern. This is a non-fetch match, that is, the match does not need to be fetched for later use. For example, "Windows(?=95|98|NT|2000)" can match "Windows" in "Windows2000", but cannot match "Windows" in "Windows3.1". Prefetching does not consume characters, that is, after a match occurs, the search for the next match begins immediately after the last match, rather than starting after the character containing the prefetch.
Forward negative pre-check, match the search string at the beginning of any string that does not match pattern. This is a non-fetch match, that is, the match does not need to be fetched for later use. For example, "Windows(?!95|98|NT|2000)" can match "Windows" in "Windows3.1", but cannot match "Windows" in "Windows2000".
Reverse positive pre-check is similar to forward positive pre-check, but in the opposite direction. For example, "(?<=95|98|NT|2000)Windows" can match "Windows" in "2000Windows", but cannot match "Windows" in "3.1Windows".
[^xyz] Negative value character set. Matches any character not included. For example, "[^abc]" would match "plin" in "plain".
[a-z]Character range. Matches any character within the specified range. For example, "[a-z]" matches any lowercase alphabetic character in the range "a" through "z". Note: Only when the hyphen is inside the character group and appears between two characters, it can represent the range of characters; if it appears at the beginning of the character group, it can only represent the hyphen itself.
[^a-z]Negative character range. Matches any character not within the specified range. For example, "[^a-z]" matches any character that is not in the range "a" through "z".
\b Matches a word boundary, which refers to the position between a word and a space. For example, "er\b" matches the "er" in "never" but not the "er" in "verb".
\B Matches non-word boundaries. "er\B" can match the "er" in "verb", but not the "er" in "never".
\cx Matches the control character specified by x. For example, \cM matches a Control-M or carriage return character. The value of x must be one of A-Z or a-z. Otherwise, treat c as a literal "c" character.
\d Matches a numeric character. Equivalent to [0-9].
\D Matches a non-numeric character. Equivalent to [^0-9].
\f Matches a form feed character. Equivalent to \x0c and \cL.
\n Matches a newline character. Equivalent to \x0a and \cJ.
\r Matches a carriage return character. Equivalent to \x0d and \cM.
\s Matches any whitespace character, including spaces, tabs, form feeds, etc. Equivalent to [ \f\n\r\t\v].
\S Matches any non-whitespace character. Equivalent to [^ \f\n\r\t\v].
\t Matches a tab character. Equivalent to \x09 and \cI.
\v Matches a vertical tab character. Equivalent to \x0b and \cK.
\w Matches any word character including an underscore. Equivalent to "[A-Za-z0-9_]".
\W Matches any non-word character. Equivalent to "[^A-Za-z0-9_]".
\xn Matches n, where n is the hexadecimal escape value. The hexadecimal escape value must be exactly two digits long. For example, "\x41" matches "A". "\x041" is equivalent to "\x04&1". ASCII encoding can be used in regular expressions.
\num Matches num, where num is a positive integer. A reference to the match obtained. For example, "(.)\1" matches two consecutive identical characters.
\nIdentifies an octal escape value or a backreference. If \n is preceded by at least n fetched subexpressions, n is a backward reference. Otherwise, if n is an octal number (0-7), then n is an octal escape value.
\nmIdentifies an octal escape value or a backreference. If there are at least nm get subexpressions before \nm, nm is a backward reference. If \nm is preceded by at least n obtains, then n is a backward reference followed by the literal m. If none of the previous conditions are met, and if n and m are both octal numbers (0-7), then \nm will match the octal escape value nm.
\nmlIf n is an octal number (0-7), and m and l are both octal numbers (0-7), match the octal escape Value nml.
\un Matches n, where n is a Unicode character represented by four hexadecimal digits. For example, \u00A9 matches the copyright symbol (©).
<?php
$weigeti=&#39;course.html 在线教程网址://m.sbmmt.com/course.html&#39;;
echo preg_replace(&#39;/course.html/&#39;,&#39;w3c&#39;,$weigeti);
//大小写不同,输出【w3c 在线教程网址://m.sbmmt.com/course.html】
echo preg_replace(&#39;/course.html/i&#39;,&#39;w3c&#39;,$weigeti);
//忽略大小写,执行替换输出【w3c 在线教程网址:http://e.php.cn/w3c/】
echo preg_replace(&#39;/网址/u&#39;,&#39;&#39;,$weigeti);
//强制 UTF-8中文,执行替换,输出【course.html 在线教程://m.sbmmt.com/course.html】
?>
Copy after login

大小写和中文在PHP中都是敏感的,但是在Javascript正则中,只对大小写敏感,忽略大小写也是通过修饰符 i 作用的,但是Javascript 不需要告知是否是UTF-8中文等特殊字符,直接可以匹配中文。

PHP 正则换行符实例

PHP 正则表达式在遇到换行符时,会将换行符当做字符串中间一个普通字符。而通用符号.不能匹配\n,所以遇到带有换行符的字符串正则会有很多要点。

<?php
$weigeti="php.cn\nIS\nLOVING\nYOU";
// 想要把上面$weigeti 替换成php.cn
echo preg_replace(&#39;/^[A-Z].*[A-Z]$/&#39;,&#39;&#39;,$weigeti);
// 这个正则表达式是,匹配只包含\w的元素,$weigeti 是以V开头,符合[A-Z],而且结尾是U,也符合[A-Z]。.无法匹配\n
// 输出【php.cn IS LOVEING YOU】
echo preg_replace(&#39;/^[A-Z].*[A-Z]$/s&#39;,&#39;&#39;,$weigeti);
// 这个用修饰符s,也就是 . 可以匹配 \n 了,所以整句匹配,输出空
// 输出【】
echo preg_replace(&#39;/^[A-Z].*[A-Z]$/m&#39;,&#39;&#39;,$weigeti);
// 这里使用了修饰符,将\n作为多行独立匹配。也就等价于:
/* 
$preg_m=preg_replace(&#39;/^[A-Z].*[A-Z]$/m&#39;,&#39;&#39;,$weigeti);
$p=&#39;/^[A-Z].*[A-Z]$/&#39;;
$a=preg_replace($p,&#39;&#39;,&#39;php.cn&#39;);
$b=preg_replace($p,&#39;&#39;,&#39;IS&#39;);
$c=preg_replace($p,&#39;&#39;,&#39;LOVING&#39;);
$d=preg_replace($p,&#39;&#39;,&#39;YOU&#39;);
$preg_m === $a.$b.$c.$d;
*/
// 输出【php.cn】
?>
Copy after login

以后您在使用PHP 抓取某个网站内容,并用正则批量替换的时候,总无法避免忽略获取的内容包含换行符,所以在使用正则替换的时候一定要注意。

PHP 正则匹配执行函数PHP 正则替换可以使用一个修饰符e,代表 eval() 来执行匹配后的内容某个函数。

<?php
$weigeti=&#39;course.html 在线教程网址://m.sbmmt.com ,你Jbzj!了吗?&#39;;
// 将上面网址转为小写
echo preg_replace(&#39;/(http\:[\/\w\.\-]+\/)/e&#39;,&#39;strtolower("$1")&#39;,$weigeti);
// 使用修饰符e之后,就可以对匹配的网址执行PHP 函数 strtolower() 了
// 输出 【course.html 在线教程网址://m.sbmmt.com ,你Jbzj!了吗?】
?>
Copy after login

根据上面代码,尽管匹配后的函数 strtolower() 在引号内,但是依旧会被eval()执行。

正则替换匹配变量向后引用

如果您熟悉Javascript,一定对$1 $2 $3 …… 等向后引用比较熟悉,而在 PHP 中这些也可以被当作向后引用参数。而在PHP中,还可以使用 \1 \\1 来表示向后引用。

向后引用的概念就是匹配一个大片段,这个正则表达式内部又被用括号切割成若干小匹配元素,那么每个匹配元素就被按照小括号序列用向后引用代替。

复制代码代码如下:

<?php
$weigeti=&#39;course.html 在线教程网址://m.sbmmt.com ,你Jbzj!了吗?&#39;;
echo preg_replace(&#39;/.+(http\:[\w\-\/\.]+\/)[^\w\-\!]+([\w\-\!]+).+/&#39;,&#39;$1&#39;,$weigeti);
echo preg_replace(&#39;/.+(http\:[\w\-\/\.]+\/)[^\w\-\!]+([\w\-\!]+).+/&#39;,&#39;\1&#39;,$weigeti);
echo preg_replace(&#39;/.+(http\:[\w\-\/\.]+\/)[^\w\-\!]+([\w\-\!]+).+/&#39;,&#39;\\1&#39;,$weigeti);
// 上面三个都是输出 【//m.sbmmt.com】
echo preg_replace(&#39;/^(.+)网址:(http\:[\w\-\/\.]+\/)[^\w\-\!]+([\w\-\!]+).+$/&#39;,&#39;栏目:$1<br>网址:$2<br>商标:$3&#39;,$weigeti);
/*
栏目:course.html 在线教程
网址://m.sbmmt.com
商标:Jbzj!
*/
// 括号中括号,外面括号先计数
echo preg_replace(&#39;/^((.+)网址:(http\:[\w\-\/\.]+\/)[^\w\-\!]+([\w\-\!]+).+)$/&#39;,&#39;原文:$1<br>栏目:$2<br>网址:$3<br>商标:$4&#39;,$weigeti);
/*
原文:course.html 在线教程网址://m.sbmmt.com ,你Jbzj!了吗?
栏目:course.html 在线教程
网址://m.sbmmt.com
商标:Jbzj!
*/
?>
Copy after login

二.php str_replace多次

1、只对needle使用数组。

示例:str_replace(array('m','i'),'n',"my name is jim!");返回:ny nane ns jnn!
可以看出,函数顺序性的对数组中每个字符串进行替换,并返回替换后的字符串。

2、只对new_needle使用数组。

示例:str_replace('m',array('n','z'),"my name is jim!\n")返回:Arrayy naArraye is jiArray!
该替换比较有意思,如果只对第二个参数使用数组则函数将其作为字符串Array进行使用,将所有的needle替换为了数组。

3、只对haystack使用数组。

示例:str_replace("m","n",array("my name is jim!","the game is over!"))该语句执行结果返回一个数组,即分别为传入的两个字符串替换后的结果。
如果输出数组内容会看到:ny nane is jin! the gane is over!

4、对needle和new_needle都使用数组。

示例:str_replace(array("m","i"),array("n","z"),"my name is jim!")返回:ny nane zs jzn!
查看执行结果可以发现,如果前两个参数都使用数组则函数把数组各个对象项字符串进行了替换,及needle的第一项替换为new_needle的第一项。以此类推。

如果needle数组比new_deedle长,例如:str_replace(array("m","i","s"),array("n","z"),"my name is jim!");返回:ny nane z jzn!可见,对于needle数组多出来的字符串被替换为了空串。
如果new_needle数组比needle长,例如:str_replace(array("m","i"),array("n","z","x"),"my name is jim!")返回ny nane zs jzn!可见new_needle多余的项被忽略。

5、三个参数都使用数组。

例如:str_replace(array("m","i"),array("n","z"),array("my name is jim!","the game is over"))返回的数组内容:ny nane zs jzn!the gane zs over
这个比较好理解,对两个字符串分别执行替换。

三.str_replace \\ \

实例

把字符串 "Hello world!" 中的字符 "world" 替换为 "Shanghai":

<?php
echo str_replace("world","Shanghai","Hello world!");
?>
Copy after login
Copy after login

定义和用法

str_replace() 函数以其他字符替换字符串中的一些字符(区分大小写)。

该函数必须遵循下列规则:

如果搜索的字符串是数组,那么它将返回数组。

如果搜索的字符串是数组,那么它将对数组中的每个元素进行查找和替换。

如果同时需要对数组进行查找和替换,并且需要执行替换的元素少于查找到的元素的数量,那么多余元素将用空字符串进行替换

如果查找的是数组,而替换的是字符串,那么替代字符串将对所有查找到的值起作用。

注释:该函数区分大小写。请使用 str_ireplace() 函数执行不区分大小写的搜索。

注释:该函数是二进制安全的。

语法

str_replace(find,replace,string,count)
Copy after login
Copy after login
参数描述
find必需。规定要查找的值。
replace必需。规定替换 find 中的值的值。
string必需。规定被搜索的字符串。
count可选。对替换数进行计数的变量。


例子 1

使用带有数组和 count 变量的 str_replace() 函数:

<?php
$arr = array("blue","red","green","yellow");
print_r(str_replace("red","pink",$arr,$i));
echo "替换数:$i";
?>
Copy after login
Copy after login

例子 2

使用带有需要替换的元素少于查找到的元素的 str_replace() 函数:

<?php
$find = array("Hello","world");
$replace = array("B");
$arr = array("Hello","world","!");
print_r(str_replace($find,$replace,$arr));
?>
Copy after login
Copy after login


四.php str_irreplace

实例

把字符串 "Hello world!" 中的字符 "world" 替换为 "Shanghai":

<?php
echo str_replace("world","Shanghai","Hello world!");
?>
Copy after login
Copy after login

定义和用法

str_replace() 函数以其他字符替换字符串中的一些字符(区分大小写)。

该函数必须遵循下列规则:

如果搜索的字符串是数组,那么它将返回数组。

如果搜索的字符串是数组,那么它将对数组中的每个元素进行查找和替换。

如果同时需要对数组进行查找和替换,并且需要执行替换的元素少于查找到的元素的数量,那么多余元素将用空字符串进行替换

如果查找的是数组,而替换的是字符串,那么替代字符串将对所有查找到的值起作用。

注释:该函数区分大小写。请使用 str_ireplace() 函数执行不区分大小写的搜索。

注释:该函数是二进制安全的。

语法

str_replace(find,replace,string,count)
Copy after login
Copy after login


参数描述
find必需。规定要查找的值。
replace必需。规定替换 find 中的值的值。
string必需。规定被搜索的字符串。
count可选。对替换数进行计数的变量。


例子 1

使用带有数组和 count 变量的 str_replace() 函数:

<?php
$arr = array("blue","red","green","yellow");
print_r(str_replace("red","pink",$arr,$i));
echo "替换数:$i";
?>
Copy after login
Copy after login

例子 2

使用带有需要替换的元素少于查找到的元素的 str_replace() 函数:

<?php
$find = array("Hello","world");
$replace = array("B");
$arr = array("Hello","world","!");
print_r(str_replace($find,$replace,$arr));
?>
Copy after login
Copy after login


五 .php str_split

PHP函数split()的基本语法为:array split ( string $pattern, string $string [, int $limit] )。我们向大家举了两个例子来具体讲解这个函数的使用方法。

对于初学者来说,掌握PHP中常用函数的用法,是其继续学习的基础。今天我们就为大家详细介绍有关PHP函数split()的一些使用方法,希望大家能通过这篇文章介绍的内容增加自己的知识库。
说明

array split ( string $pattern, string $string [, int $limit] )
Copy after login

提示
preg_split() 函数使用了 Perl 兼容正则表达式语法,通常是比PHP函数split()更快的替代方案。如果不需要正则表达式的威力,则使用 explode() 更快,这样就不会招致正则表达式引擎的浪费。
本函数返回一个字符串数组,每个单元为 string 经区分大小写的正则表达式 pattern 作为边界分割出的子串。如果设定了 limit,则返回的数组最多包含 limit 个单元,而其中最后一个单元包含了 string 中剩余的所有部分。如果出错,则 split() 返回 FALSE。
将 /etc/passwd 中的前四个字段分割出来:
例 1839. PHP函数split()例子
深入PHP nl2br()格式化输出的详解

<?php list($user, $pass, $uid, $gid, $extra) =  split (":", $passwd_line, 5);  ?>
Copy after login

如果字符串中有 n 个与 pattern 匹配的项目,则返回的数组将包含 n+1 个单元。例如,如果没有找到 pattern,则会返回一个只有一个单元的数组。当然,如果 string 为空也是这样。
解析可能用斜线,点,或横线分割的日期:
例 1840. PHP函数split()例子

<?php 
   // 分隔符可以是斜线,点,或横线 
   $date = "04/30/1973";  
   list($month, $day, $year) = split (&#39;[/.-]&#39;, $date); 
   echo "Month: $month; Day: $day; Year: $year<br />\n";
?>
Copy after login

想仿效 Perl 中类似的 @chars = split('', $str) 行为,请参考 preg_split() 或 str_split() 函数中的例子。
注意 pattern 是一个正则表达式。如果想要用的分割字符是正则表达式中的特殊字符,要先将其转义。如果觉得PHP函数split()(或其它任何 regex 函数)行为古怪的话,请阅读包含在 PHP 发行包中 regex/ 子目录下的 regex.7 文件。该文件是手册页面格式,可以用类似 man /usr/local/src/regex/regex.7 的命令来阅读

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