0

I'm loading flies in this format:

///mvid:417815 qty:2 name:Aether Hub loc:Deck
2 Aether Hub
///mvid:423770 qty:2 name:Aetherstream Leopard loc:Deck
2 Aetherstream Leopard
///mvid:401837 qty:4 name:Canopy Vista loc:Deck
4 Canopy Vista
///mvid:426709 qty:2 name:Cartouche of Solidarity loc:Deck
2 Cartouche of Solidarity

What I'd like to do is load into an array where I have the following structure (I need to ignore every other line as well):

417815|2|Aether Hub|Deck
423770|2|Aetherstream Leopard|Deck
401837|4|Canopy Vista|Deck
426709|2|Cartouche of Solidarity|Deck
3
  • 3
    where is code ? Commented May 8, 2017 at 6:33
  • php.net/manual/en/function.file.php Commented May 8, 2017 at 6:34
  • What have you tried already? Do show some effort rather than just dumping complete problems/assignments here. Commented May 8, 2017 at 7:32

3 Answers 3

1

I'm not sure where you are getting that format for loading but if you have it in a variable here's the code you can use to achieve want you said.

<?php
    $values = [];
    $content = "///mvid:417815 qty:2 name:Aether Hub loc:Deck
2 Aether Hub
///mvid:423770 qty:2 name:Aetherstream Leopard loc:Deck
2 Aetherstream Leopard
///mvid:401837 qty:4 name:Canopy Vista loc:Deck
4 Canopy Vista
///mvid:426709 qty:2 name:Cartouche of Solidarity loc:Deck
2 Cartouche of Solidarity";

    preg_match_all("#mvid:(\d+)\s+qty:(\d+)\s+name:([^:]+):(\w+)#", $content, $matches);

    $count = count($matches[1]); // we store the count of the elements so that we don't call the function in every iteration of for loop

    for($i = 0; $i < $count; $i++) {
        $values[$i] = $matches[1][$i]."|".$matches[2][$i]."|".$matches[3][$i]."|".$matches[4][$i];
    }

    echo "<pre>".print_r($values, true);

Output:- https://eval.in/789124

Sign up to request clarification or add additional context in comments.

1 Comment

nice answer.+1 for worthy effort
0

Here we are using preg_split to split on the basis of regular expression

Try this code snippet here

<?php
$string='///mvid:417815 qty:2 name:Aether Hub loc:Deck
2 Aether Hub
///mvid:423770 qty:2 name:Aetherstream Leopard loc:Deck
2 Aetherstream Leopard
///mvid:401837 qty:4 name:Canopy Vista loc:Deck
4 Canopy Vista
///mvid:426709 qty:2 name:Cartouche of Solidarity loc:Deck
2 Cartouche of Solidarity';
$lines=explode("\n", $string);//optionally you can use file function to get the array of lines.
$result=array();
for($x=0;$x<count($lines);$x+=2)
{
    $split=preg_split("/[a-z]+:/",$lines[$x]);
    unset($split[0]);
    $result[]=implode("|",array_map("trim",array_values($split)));
}
print_r($result);

Output:

Array
(
    [0] => 417815|2|Aether Hub|Deck
    [1] => 423770|2|Aetherstream Leopard|Deck
    [2] => 401837|4|Canopy Vista|Deck
    [3] => 426709|2|Cartouche of Solidarity|Deck
)

Comments

0

Thanks that got me the info I needed to split the file up and load pieces into the array. From the array I could then make the form to select the appropriate cards to be displayed.

This is how I loaded the file:

$deck1 = [];
$maincount = 0;
$handle = @fopen("Deck1.dec", "r");
if ($handle) {
    while (($buffer = fgets($handle, 4096)) !== false) {
    preg_match_all("#mvid:(\d+)\s+qty:(\d+)\s+name:([^:]+):(\w+)#", $buffer, $matches);
    $count = count($matches[1]); 
    for($i = 0; $i < $count; $i++) {
        $deck1[$maincount][1] = $matches[1][$i];
        $deck1[$maincount][2] = $matches[2][$i];
        $deck1[$maincount][3] = rtrim($matches[3][$i], " loc");
        $deck1[$maincount][4] = $matches[4][$i];
    }
    $maincount++;
    }
    //echo "<pre>".print_r($deck1, true);
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}

And this is how I created radio buttons. I needed to remove certain cards globally.

foreach ($deck1 as $index=>$option) {
    if ($option[3] != "Plains" && $option[3] != "Forest" && $option[3] != "Mountain" && $option[3] != "Swamp" && $option[3] != "Island"){
        if ($option[4] == "SB"){
            echo "<i><input type=\"radio\" name='mText7bgo' value=\"{$option[1]}\">{$option[3]}</i><br>";
        }
        else {
            echo "<input type=\"radio\" name='mText7bgo' value=\"{$option[1]}\">{$option[3]}<br>";
        }
    }
}

I'm sure there's some optimizing that could have been done, but this got the job done. Thanks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.