在 str_replace 的嵌套 FOR 循环中显示数组值,其中数组值是替换的一部分

show an array values in nested FOR loop of str_replace where array values are part of replacement

我正在尝试替换这些标签 [mtgcard] cardname [/mtgcard] 在一个字符串中,但在替换时我还希望 cardname 成为超链接的一部分(下面的示例) 这是我用来从字符串中获取 CARDNAMES 的函数(在 Whosebug 上找到):

function getContents($str, $startDelimiter, $endDelimiter) {
  $contents = array();
  $startDelimiterLength = strlen($startDelimiter);
  $endDelimiterLength = strlen($endDelimiter);
  $startFrom = $contentStart = $contentEnd = 0;
  while (false !== ($contentStart = strpos($str, $startDelimiter, $startFrom))) {
    $contentStart += $startDelimiterLength;
    $contentEnd = strpos($str, $endDelimiter, $contentStart);
    if (false === $contentEnd) {
      break;
    }
    $contents[] = substr($str, $contentStart, $contentEnd - $contentStart);
    $startFrom = $contentEnd + $endDelimiterLength;
  }
  return $contents;
}

它工作正常,下面是我将替换标签的字符串:

        $string  = "we have a card  [mtgcard]tarmogoyf[/mtgcard] ";
        $string .= "and [mtgcard]forest[/mtgcard] ";


    //here i get all the values between [mtgcard] [/mtgcard]
    $arr = getContents($string, '[mtgcard]', '[/mtgcard]');

这给了我 Array ( [0] => tarmogoyf [1] => forest )

    //I count them for te loop  
        $count = count($arr);

        for($i = 0; $i < $count; $i++) {
//here I replace the [mtgcard] with <a href='https://deckbox.org/mtg/*HERE SHOULD BE THE value between tags*'> 
//and [/mtgcard] with </a>
            $string = str_ireplace(array("[mtgcard]", "[/mtgcard]"),array("<a href='https://deckbox.org/mtg/'>", "</a>"), $string);
            $arr[$i]++;
        }
        echo $string;

以上脚本显示:

we have a card 
 1. <a href="https://deckbox.org/mtg/">tarmogoyf</a>
 2. <a href="https://deckbox.org/mtg/">forest</a>

这正是我想要的,但部分原因是我想完成与卡名的超链接,以便为 exmaple 提供正确的路径 https://deckbox.org/mtg/cardname

为此尝试了上述 FOR 循环并进行了以下更改:

$count = count($arr);
    for($i = 0; $i < count($arr); $i++) {
        $string = str_ireplace(array("[mtgcard]", "[/mtgcard]"),array("<a href='https://deckbox.org/mtg/$arr[$i]'>", "</a>"), $string);
        $arr[$i]++;
    }

我得到了这个结果:

 1. <a href="https://deckbox.org/mtg/tarmogoyf">tarmogoyf</a>
 2. <a href="https://deckbox.org/mtg/tarmogoyf">forest</a>

所有超链接的第一个值都是 array($arr),我也尝试了嵌套的 foreach 循环,但输出重复了两倍。我想要的是:

1. <a href="https://deckbox.org/mtg/tarmogoyf">tarmogoyf</a>
     2. <a href="https://deckbox.org/mtg/forest">forest</a>

我们也会接受任何关于工作良好的脚本以使其更好的建议。

如果您只想替换字符串中的 [mtgcard] 元素,使用 preg_replace 进行基于正则表达式的替换最容易实现:

$string  = "we have a card  [mtgcard]tarmogoyf[/mtgcard] ";
$string .= "and [mtgcard]forest[/mtgcard] ";

$string = preg_replace('/\[mtgcard\](.*?)\[\/mtgcard\]/', '<a href="https://deckbox.org/mtg/"></a>', $string);
echo $string;

输出:

we have a card  <a href="https://deckbox.org/mtg/tarmogoyf">tarmogoyf</a> and <a href="https://deckbox.org/mtg/forest">forest</a> 

如果要生成所有[mtgcard]元素的数组,可以使用preg_match_all查找元素的内容,然后将必要的片段拼接成链接即可:

$string  = "we have a card  [mtgcard]tarmogoyf[/mtgcard] ";
$string .= "and [mtgcard]forest[/mtgcard] ";

preg_match_all('/\[mtgcard\](.*?)\[\/mtgcard\]/', $string, $matches);
$arr = $matches[1];
foreach ($arr as &$a) {
    $a = "<a href=\"https://deckbox.org/mtg/$a\">$a</a>";
}
print_r($arr);

输出:

Array (
    [0] => <a href="https://deckbox.org/mtg/tarmogoyf">tarmogoyf</a>
    [1] => <a href="https://deckbox.org/mtg/forest">forest</a>
)

Demo on 3v4l.org