PHP preg_replace()

PHPz
发布: 2024-08-29 12:49:57
原创
538 人浏览过

PHP preg_replace() 函数是 PHP 编程语言中的内置函数。 Preg_replace() 函数有助于执行正则表达式来搜索和替换内容。可以对数组索引值内部的单个字符串或多个字符串元素进行替换。它在某种程度上类似于 preg_match(),但在 preg_replace() 中,在对字符串执行模式匹配后,将对特定文本替换字符串元素。这是 PHP 编程语言中用于操作字符串内容的重要正则表达式之一。下面您将详细了解 preg_replace()。

广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法

语法如下:

preg_replace( $pattern1, $replacement1, $subject1, $limit1, $count1 )
登录后复制

参数

preg_replace() 函数通常主要接受上面提到的五个参数。它们是 $pattern1、$replacement1、$subject1、$limit1 和 $count1 参数。

了解这些参数,并在下面简要说明。

  • $pattern1:preg_replace() 函数的 $pattern1 参数包含字符串元素,有助于搜索内容。它可以是单个字符串值,也可以是字符串数组。
  • Sreplacement1: preg_replace() 函数的$replacement1 参数是强制参数。指定字符串或带有一些要替换的字符串的字符串数组会很有帮助。如果此参数具有字符串值,则pattern1参数将是一个数组,并且所有模式将被该特定字符串替换。如果 replacement1 和pattern1 参数都是数组,则每个pattern1 都将被replacement1 对应项替换。如果 replacement1 是一个数组,并且它包含的元素少于 pattern1 数组,那么任何额外的模式都将被简单的空字符串替换。
  • $subject1:preg_replace() 函数的 $subject1 参数是一个字符串或字符串数​​组,用于从 $pattern 变量的字符串值中搜索并替换字符串内容。
  • $limit1:preg_replace() 函数的 $limit 参数非常重要,它指定每个模式最多可能的替换。
  • $count1:preg_replace() 函数的 $count 参数通常是可选参数。该参数的变量值用数字填充,该数字表示在原始字符串中完成的替换次数。

preg_replace() 函数的返回值:仅当 subect1 参数是数组/字符串时,preg_replace() 函数才会返回数组。

PHP preg_replace() 函数如何工作?

PHP 编程语言的 Preg_replace() 函数基于其中提到的参数来工作。它主要通过使用搜索时使用的原始字符串和替换字符串来工作。其他参数可能并不那么重要。 Preg_replace() 将采用正则表达式作为参数(第一个参数),用于将每个匹配项替换为 2nd 参数。第二个参数是纯文本,而 3rd 参数是要工作的字符串。第三个参数可以包含 $n ,它是插入作为正则表达式规则的一部分匹配的文本。如果我们正在编写一些复杂的多部分正则表达式。您可以使用 $0 来使用匹配的文本。查看示例,以便您在下面的示例 2 中了解。

在 PHP 中实现 preg_replace() 函数的示例

下面是一些提到的例子:

示例#1

在下面的示例中,使用值“Copyright 1998”创建变量“$copy_date1”,然后使用 preg_replace() 函数作为值创建相同的变量。因此 preg_replace() 函数的结果将存储在“$copy_date1”变量中。在 preg_replace() 函数中,值 0-9(全部)将被字符串值“2020”替换并存储在“$copy_date1”变量中。然后将打印“$copy_date1”中存在的值,即更改后的字符串。然后“Copyright 2020”将打印在输出中。

代码:

<?php
$copy_date1 = "Copyright 1998";
$copy_date1 = preg_replace("([0-9]+)", "2020", $copy_date1);
print $copy_date1;
?>
登录后复制

输出:

PHP preg_replace()

Example #2

In the below example, a variable “$a1” is created with the string value “Fool bool tool fool”. Then again the same naming variable is created with the preg_replace() function. In the preg_replace() string value is used to search which are ending with a cool after any alphabet. If there is anything then those string values will be printed one by one after the “Got word:” word. And this result will be stored in the “$a1” variable. Then the word will be printed using the print function.

Code:

<?php
$a1 = "Foo mool bool tool fool";
$a1 = preg_replace("/[A-Za-z]ool\b/", "Got word: $0 \n", $a1);
print $a1;
?>
登录后复制

Output:

PHP preg_replace()

Example #3

In the below example, the date will be changed in the string value as mentioned in the replacement1 term. At first a variable “$date1” is created inside of the PHP tags with the value “Sep 29, 2020”. Then the “$pattern1” variable is created with meta-characters that are used to find a word in the character. Then the “$replacement1” variable is created with the value ”${1} 6, $3”. Here “1” inside flower braces will remain the same and then in the 2nd value 6 will be replaced in the place of 29 and $3 represents the 3 string elements inside of the array. If “$1 ” is placed then only Sep will be printed if the $2 is represented then replaced value only 6 will be printed. So the $3 is mentioned to mention all the 3 elements which are to be printed using echo function.

Code:

<?php
$date1 = 'Sep 29, 2020';
$pattern1 = '/(\w+) (\d+), (\d+)/i';
$replacement1 = '${1} 6,$3';
echo preg_replace($pattern1, $replacement1, $date1);
?>
登录后复制

Output:

PHP preg_replace()

Example #4

In the below example, multiple string elements are replaced using the array function with the single string values in the index values to change. At first “string1” variable is created with a string sentence with many words. Then “$patterns1” variable is created with array() function as variable. Then inside of the $patterns[index value] is/are stored with a different string that is already present in the original string. Similarly the same is done for the “$replacements1” variable with different string values to change the list of strings inside of the string sentence. Then “$newstr1” variable is created with the value as preg_replace() function. Then echo function is used to print the changed string sentence. Check out this in the output image.

Code:

<?php
$string1 = 'The fast black tiger runs away from the zoo.';
$patterns1 = array();
$patterns1[0] = '/fast/';
$patterns1[1] = '/black/';
$patterns1[2] = '/tiger/';
$replacements1 = array();
$replacements1[2] = 'Slow';
$replacements1[1] = 'Elephant';
$replacements1[0] = 'Jumped and';
$newstr1 = preg_replace($patterns1, $replacements1, $string1);
echo "<b>The String after the replacements:</b> " .$newstr1;
?>
登录后复制

Output:

PHP preg_replace()

Conclusion

I hope you understood what is the definition of PHP preg_replace() function with its syntax along with various parameter explanation, How preg_replace() function works along with the various examples to implement preg_replace() function in PHP Programming language.

以上是PHP preg_replace()的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
php
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!