替换字符串的多个部分

时间:2013-09-16 17:34:14

标签: php str-replace

假设我有以下例子:

$string = "^4Hello World ^5Foo^8Bar";

我目前正在使用以下功能来替换插入符号和相邻的数字,这是我能够达到的最接近的数字。

function addColours($input) {
    $var = $input;
    $var = str_replace('^0', '</span><span style="color: #000000">', $var);
    $var = str_replace('^1', '</span><span style="color: #ff0000">', $var);
    $var = str_replace('^2', '</span><span style="color: #00ff00">', $var);
    $var = str_replace('^3', '</span><span style="color: #ffff00">', $var);
    $var = str_replace('^4', '</span><span style="color: #0000ff">', $var);
    $var = str_replace('^5', '</span><span style="color: #00ffff">', $var);
    $var = str_replace('^6', '</span><span style="color: #ff00ff">', $var);
    $var = str_replace('^7', '</span><span style="color: #ffffff">', $var);
    $var = str_replace('^8', '</span><span style="color: #CC9933">', $var);
    $var = str_replace('^9', '</span><span style="color: #8D8D8D">', $var);
    return $var;
}

这会为字符串返回以下内容。

</span><span style="color: #0000ff">Hello World </span><span style="color: #00ffff">Foo</span><span style="color: #CC9933">Bar

它适用于字符串的中间部分,但显然它在字符串的开头添加了一个不需要的</span>,并且它没有关闭结束标记。

有没有办法使这项工作?

谢谢,Ben。

4 个答案:

答案 0 :(得分:1)

那怎么样?

function addColours($input) {
    $out = '';
    $replacements = array(
            '1' => '000000',
            '2' => 'ff0000'
    );

    foreach(explode('^', $input) as $span) {
        if(in_array($span[0], array_keys($replacements))) {
            $out .= '<span style="color: #' .  $replacements[$span[0]] . '">' . substr($span, 1) . '</span>';
        } else {
            $out .= $span;
        }
    }

    return $out;
}

$body = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. ^1Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. ^2It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";

print addColours($body);

答案 1 :(得分:1)

这就是我在评论中所说的:

$colorMap = array(
  '000000', // 0
  'ff0000', // 1
  '00ff00', // 2...     
);

$template = '<span style="color:#%s">%s</span>';

// split by "^" followed by 0-9
$parts = preg_split('/\^(\d)/', $string, -1,
            PREG_SPLIT_NO_EMPTY + PREG_SPLIT_DELIM_CAPTURE);

$result = '';

// rebuild string (odd elements are delimiters)
foreach($parts as $idx => $part)
  if((($idx % 2) === 0) && isset($colorMap[(int)$part]))
    $result .= sprintf($template, $colorMap[(int)$part], $parts[$idx + 1]);

答案 2 :(得分:0)

你真的需要运行preg替换,而不是strreplace。你仍然会遇到的问题是你需要找到一个模式,如'^ 4Some Sentance ^',这样你就可以正确地结束跨度。但如果条目是最后一次使用标签并且未关闭,会发生什么?它会失败。

我建议切换到这种格式:

[4]Some sentence[/4]

然后你可以正确运行一个简单的str_replace:

function addColours($input) {
    $replacements = array(
            '1' => '000000',
            '2' => 'ff0000'
    );

    foreach($replacements as $key => $value) {
        $input = str_replace("[$key]", '<span style="color: #' . $value . '">');
        $input = str_replace("[/$key]", '</span>');
    }

    return $input;
}

答案 3 :(得分:0)

这是我的两分钱。它最接近OP代码,不需要手动重建字符串。

function addColours($input) {

  $var = $input;

  $var = preg_replace('/\^0(.*?)((\^\d)|($))/', '<span style="color: #000000">$1</span>$2', $var);
  $var = preg_replace('/\^1(.*?)((\^\d)|($))/', '<span style="color: #ff0000">$1</span>$2', $var);
  $var = preg_replace('/\^2(.*?)((\^\d)|($))/', '<span style="color: #00ff00">$1</span>$2', $var);
  $var = preg_replace('/\^3(.*?)((\^\d)|($))/', '<span style="color: #ffff00">$1</span>$2', $var);
  $var = preg_replace('/\^4(.*?)((\^\d)|($))/', '<span style="color: #0000ff">$1</span>$2', $var);
  $var = preg_replace('/\^5(.*?)((\^\d)|($))/', '<span style="color: #00ffff">$1</span>$2', $var);
  $var = preg_replace('/\^6(.*?)((\^\d)|($))/', '<span style="color: #ff00ff">$1</span>$2', $var);
  $var = preg_replace('/\^7(.*?)((\^\d)|($))/', '<span style="color: #ffffff">$1</span>$2', $var);
  $var = preg_replace('/\^8(.*?)((\^\d)|($))/', '<span style="color: #CC9933">$1</span>$2', $var);
  $var = preg_replace('/\^9(.*?)((\^\d)|($))/', '<span style="color: #8D8D8D">$1</span>$2', $var);

  return $var;

}