0

假设我有以下示例:

$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>在字符串的开头添加了一个不需要的标签,并且没有关闭结束标签。

有什么办法可以使这项工作?

谢谢,本。

4

4 回答 4

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]);
于 2013-09-16T18:09:40.720 回答
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);
于 2013-09-16T18:01:55.600 回答
0

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

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;

}
于 2013-09-16T18:22:53.990 回答
0

您确实需要运行 preg replace,而不是 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;
}
于 2013-09-16T17:47:05.970 回答