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

时间:2014-09-24 10:04:31

标签: php

$message = "[Link] Thanks [Link]";
echo str_replace(
array("[Link]", "[Link]"), 
array("mylink1", "mylink2"), 
$message
);

打印:" mylink1感谢mylink1"

但是,我想打印" mylink1谢谢mylink2"

这样做的正确方法是什么?

6 个答案:

答案 0 :(得分:3)

您可以在代码中将此功能用作替代品。它使用vsprintf作为@ h2ooooooo演示。我的早期版本失败了,因为我以错误的方式使用了$count str_replace参数。

function str_replace_array($search, array $replace, $subject)
{
    return vsprintf(
      str_replace('[Link]', '%s', $subject),
      $replace);
}

$message = "[Link] Thanks [Link]";
echo str_replace_array(
   "[Link]",
   array("mylink1", "mylink2"), 
   $message
);

答案 1 :(得分:3)

也许您正在搜索sprintf

$message = "%s Thanks %s";
echo sprintf($message, "mylink1", "mylink2"); // mylink1 Thanks mylink2

DEMO


如果您更喜欢使用数组(对动态变量很有用),那么vsprintf

$message = "%s Thanks %s";
echo vsprintf($message, array("mylink1", "mylink2")); // mylink1 Thanks mylink2

DEMO


如果确实希望不使用sprintfvsprintf,您可以使用preg_replace第4个参数(limit) #34; 1" (只在你找到的第一场比赛中这样做)。请务必将其与preg_quote一起使用,以便.+*等没有特殊含义:

<?php

function smartReplace($search, $replace, $subject) {
    if (!is_array($search)) {
        throw new Exception('$search must be an array');
    }

    if (!is_array($replace)) {
        throw new Exception('$replace must be an array');
    }

    if (!is_string($subject)) {
        throw new Exception('$subject must be a string');
    }

    for ($i = 0, $len = count($search); $i < $len; $i++) {
        $subject = preg_replace('/' . preg_quote($search[$i], '/') . '/', $replace[$i], $subject, 1);
    }

    return $subject;
}

$message = "[Link] Thanks [Link]";

echo smartReplace(
    array("[Link]", "[Link]"), 
    array("mylink1", "mylink2"), 
    $message
); // mylink1 Thanks mylink2

DEMO

答案 2 :(得分:0)

您可以使用preg_replace代替str_replace来设置限额

$message = "[Link] Thanks [Link]";
$replacements = array ("Link 1", "Link 2");
while (!empty($replacements)) {
  $replacement = array_shift($replacements);
  $message = preg_replace("/\[Link\]/", $replacement, $message, 1);
}
echo $message;

输出:

  

链接1感谢链接2

答案 3 :(得分:0)

一种简单的方法是合并str_replacevsprintf

$message = "[Link] Thanks [Link]";
$template = str_replace("[Link]", "%s", $message);
echo vsprintf($template, array("mylink1", "mylink2"));

答案 4 :(得分:0)

试试这个

$message = "[Link] Thanks [Link]";
echo preg_replace(array("/\[Link\]/", "/\[Link\]/"), 
array("mylink1", "mylink2"), 
$message, 1);

答案 5 :(得分:0)

如果您的邮件中包含%,则使用vsprintf可能会有害。在将%替换为%%之前,您可以将所有[Link]替换为%s,然后调用vsprintf。但我会选择简单的数组函数,这样你就不用担心正确转义字符了。

function str_replace_array($message, $search, $replacements)
{
    $result = '';

    $pieces = explode($search, $message);
    $replace = reset($replacements);
    foreach ($pieces as $i => $piece) {
        if ($replace === FALSE) {
            throw new Exception('There are not enough replacements for all occurences of the search string.');
        }
        if ($i !== 0) {
            $result .= $replace;
            $replace = next($replacements);
        }
        $result .= $piece;
    }

    return $result;
}

echo str_replace_array('[Link] Thanks [Link]', '[Link]', array('mylink1', 'mylink2'));