• 日常搜索
  • 百度一下
  • Google
  • 在线工具
  • 搜转载

如何在PHP中替换字符串

在许多应用程序中,查找和替换一段文本是一项非常常见的任务。当我们在 php 中处理文本时,也经常需要它。因此,本教程将重点介绍在 PHP 中用另一个字符串替换一个字符串。

用 PHP替换字符串str_replace()

像很多东西一样,PHP 也有一个内置函数,您可以使用它来替换 PHP 中的字符串。该str_replace($search, $replace, $subject, $count)函数采用四个参数来确定函数的行为方式。

第一个参数是$search。它用于指定您要搜索替换的字符串。将替换原始字符串的字符串使用 指定$replace。该$subject参数是主字符串或干草堆,您将在其中进行所有替换。两者的值$search和$replace可以是数组或字符串。

在 PHP 中用另一个字符串替换一个字符串

当我们只想用其他东西替换单个字符串时,$search我们可以简单地提供一个字符串值。$replace

<?php

$sentence = "Andrew prefers tea to coffee.";

$search = "Andrew";
$replace = "Adam";

$new_sentence = str_replace($search, $replace, $sentence);

// Adam prefers tea to coffee.
echo $new_sentence;

?>

用单个字符串替换多个字符串

正如我之前提到的,您可以将 的值指定$search为一个数组。如果$replace是单个字符串,那么$search数组中的所有值都将替换为$replace我们的主字符串。

<?php

$sentence = "There were 60 bananas and 12 oranges in the first basket. Some children ate 2 mangoes each.";

$search = ["bananas", "mangoes"];
$replace = "apples";

$new_sentence = str_replace($search, $replace, $sentence);

// There were 60 apples and 12 oranges in the first basket. Some children ate 2 apples each.
echo $new_sentence;

?>

从字符串中删除字符或子字符串

从字符串中删除字符或子字符串只需将值设置为$replace空字符串即可。

<?php

$sentence = "Andrew, Monty and Adam will be meeting at the club today.";

$search = [", Monty"];
$replace = "";

$new_sentence = str_replace($search, $replace, $sentence);

// Andrew and Adam will be meeting at the club today.
echo $new_sentence;

?>

替换某些子字符串并从字符串中删除其他子字符串

两者$search和$replace都可以指定为数组。在这种情况下,in 中的第一个元素$search将被 in 中的第一个元素替换,$replace依此类推。换句话说,in 中的子字符串$search将被$replace相应索引处的子字符串 in 替换。

如果$search和$replace数组的长度不相等怎么办?如果$search元素多于$replace,则 in 中的额外元素$search将被空字符串替换。

<?php

$sentence = "There were 60 bananas and 12 oranges in the first basket. Some children ate 2 mangoes each.";

$search = ["bananas", "mangoes"];
$replace = ["apples"];

$new_sentence = str_replace($search, $replace, $sentence);

// There were 60 apples and 12 oranges in the first basket. Some children ate 2  each.
echo $new_sentence;

?>

这个例子与我们上面写的用苹果代替香蕉和芒果的例子非常相似。这次唯一的区别是现在是一个包含单个元素的数组。但是,这会导致完全不同的结果,因为mangoes现在将被空字符串替换。$replace

计算更换次数

您可以将第四个参数传递给str_replace(). 第四个参数的值设置为执行的替换次数str_replace()。它在各种情况下都有帮助。例如,如果您事先知道应该由 执行多少次替换,那么您str_replace()可以使用设置的值str_replace()来验证没有意外的替换。

<?php

$sentence = "The house was filled with apples. There were apples in the fridge and there were apples on the dining table.
There were multiple baskets of apples in the living room. We were eating apples for breakfast, lunch and dinner.";

$search = "apples";
$replace = "mangoes";
$count = 0;

$new_sentence = str_replace($search, $replace, $sentence, $count);

// The house was filled with mangoes. There were mangoes in the fridge and there were mangoes on the dining table.
// There were multiple baskets of mangoes in the living room. We were eating mangoes for breakfast, lunch and dinner.
echo $new_sentence;

// 5
echo $count;

?>

需要牢记的要点

就像许多其他函数一样,PHP 带有一个不区分大小写的str_replace()被调用版本str_ireplace()。它的工作原理与此类似str_replace(),但以不区分大小写的方式进行替换。

<?php

$sentence = "I ate one aPPle and he ate three Apples.";

$search = "apple";
$replace = "banana";

$new_sentence = str_ireplace($search, $replace, $sentence);

// I ate one banana and he ate three bananas.
echo $new_sentence;

?>

使用 时str_replace(),请确保要替换的字符串不是较大单词的一部分。这可能会导致一些意想不到的结果。这是一个例子:

<?php

$sentence = "My nana was eating a banana.";

$search = "nana";
$replace = "friend";

$new_sentence = str_replace($search, $replace, $sentence);

// My friend was eating a bafriend.
echo $new_sentence;

?>

最后的想法

在本教程中,我们介绍了在 PHP 中替换字符串时可能出现的不同情况。该str_replace()功能非常适合任何此类基本替换。但是,重要的是要小心我们提供给不同参数的值。否则,我们将得到一些意想不到的结果,如示例中所示。您应该考虑使用该preg_replace()函数进行更复杂的字符串替换。


文章目录
  • 用 PHP替换字符串str_replace()
    • 在 PHP 中用另一个字符串替换一个字符串
    • 用单个字符串替换多个字符串
    • 从字符串中删除字符或子字符串
    • 替换某些子字符串并从字符串中删除其他子字符串
  • 计算更换次数
  • 需要牢记的要点
  • 最后的想法