Skip to content
Advertisement

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

I’m trying to replace these tags [mtgcard] cardname [/mtgcard] in a string, but while replacing I want also the cardname to be the part of an hyperlink (example below) Here is the function (found on stackoverflow) I use to get the CARDNAMES from the string:

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;
}

and It does work fine, below is the string where I’ll replace tags:

        $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]');

This gives me 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;

the above script shows:

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

And this is perfectly what i wanted but in part as I want to finish the hyperlink with the cardname to have a correct path for exmaple https://deckbox.org/mtg/cardname

For this tried the above FOR loop with these changes:

$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]++;
    }

And I get this result:

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

All hyperlinks have the first value of array($arr) and I tried also the nested foreach loop but the output repeats double time. what I want is :

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

Any suggestion would be accepted also on working fine script to make it better.

Advertisement

Answer

If you want to just replace the [mtgcard] elements in the string, this is most easily achieved using preg_replace to do a regular expression-based replacement:

$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;

Output:

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

If you want to generate an array of all the [mtgcard] elements, you can use preg_match_all to find the contents of the elements, and then just concatenate the necessary pieces to convert them into links:

$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);

Output:

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

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement