Php 在str_replace的嵌套FOR循环中显示数组值,其中数组值是替换的一部分
我试图以字符串形式替换这些标签[mtgcard]cardname[/mtgcard],但在替换时,我还希望cardname成为超链接的一部分(下面的示例) 下面是我用来从字符串中获取卡片名的函数(在stackoverflow上找到):Php 在str_replace的嵌套FOR循环中显示数组值,其中数组值是替换的一部分,php,arrays,for-loop,foreach,str-replace,Php,Arrays,For Loop,Foreach,Str Replace,我试图以字符串形式替换这些标签[mtgcard]cardname[/mtgcard],但在替换时,我还希望cardname成为超链接的一部分(下面的示例) 下面是我用来从字符串中获取卡片名的函数(在stackoverflow上找到): function getContents($str, $startDelimiter, $endDelimiter) { $contents = array(); $startDelimiterLength = strlen($startDelimiter
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]');
这给了我数组([0]=>tarmogoyf[1]=>forest)
//我为te循环计算它们
$count=计数($arr);
对于($i=0;$i<$count;$i++){
//在这里,我将[mtgcard]替换为
$string=str_ireplace(数组(“[mtgcard]”,“[/mtgcard]”),数组(“”,$string);
$arr[$i]++;
}
echo$字符串;
上面的脚本显示:
we have a card
1. <a href="https://deckbox.org/mtg/">tarmogoyf</a>
2. <a href="https://deckbox.org/mtg/">forest</a>
我们有一张卡片
1.
2.
这正是我想要的,但部分原因是我想用cardname完成超链接,以便为exmaple找到正确的路径
为此,尝试了上面的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]++;
}
$count=count($arr);
对于($i=0;$i
我得到了这个结果:
1. <a href="https://deckbox.org/mtg/tarmogoyf">tarmogoyf</a>
2. <a href="https://deckbox.org/mtg/tarmogoyf">forest</a>
1。
2.
所有超链接的第一个值都是array($arr),我也尝试了嵌套的foreach循环,但输出重复两次。我想要的是:
1. <a href="https://deckbox.org/mtg/tarmogoyf">tarmogoyf</a>
2. <a href="https://deckbox.org/mtg/forest">forest</a>
1。
2.
任何关于使用fine script使其更好的建议都将被接受。如果您只想替换字符串中的
[mtgcard]
元素,使用基于正则表达式的替换最容易实现这一点:
$string = "we have a card [mtgcard]tarmogoyf[/mtgcard] ";
$string .= "and [mtgcard]forest[/mtgcard] ";
$string = preg_replace('/\[mtgcard\](.*?)\[\/mtgcard\]/', '<a href="https://deckbox.org/mtg/$1">$1</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>
Array (
[0] => <a href="https://deckbox.org/mtg/tarmogoyf">tarmogoyf</a>
[1] => <a href="https://deckbox.org/mtg/forest">forest</a>
)
数组(
[0] =>
[1] =>
)
这回答了你的问题吗?如果没有,你能提供更多的信息来帮助回答这个问题吗?否则,请考虑标记接受的答案(在上/下投票箭头下的复选标记)。你好,尼克,是的,你的问题回答了我的问题,这正是我想要的。谢谢你宝贵的帮助。
$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>
)