每行获取2个内容

时间:2014-03-15 01:38:05

标签: php

我想知道我可以从这段代码中获取这些ID和名称:

    stuffyd[1] = {name: "Item 1"};
stuffyd[2] = {name: "Item 2"};
stuffyd[3] = {name: "Item 3"};
stuffyd[4] = {name: "Item 4"};
stuffyd[5] = {name: "Item 5"};
stuffyd[6] = {name: "Item 6"};

ID在[IDHERE]和名称中,显然是在名称之后: 我尝试使用爆炸和循环但是效果不佳。这是我的代码:

    <?php
function getItemName($i) {
    $itemNameFile = file_get_contents('test.txt');
    $itemName = explode('"', $itemNameFile);
    return $itemName[$i];
}
function getItemID($i) {
    $itemIDFile = file_get_contents('test.txt');
    $itemID1 = explode('stuffyd[', $itemIDFile);
    $itemID = explode(']', $itemID1[$i]);
    return $itemID[0];
}
function getNamesID() {
    for($i = 1; $i < 6; $i++) {
        //if ($i & 1) {
            $itemName1 = getItemName($i);
        //}
        //if($i < 6) {
            $itemID1 = getItemID($i);
        //}
    }
    echo $itemName1 . ":" . $itemID1 . chr(10);
}
getNamesID();
?>

这是输出: 项目3:5

我该怎么办?

3 个答案:

答案 0 :(得分:2)

// read the file once only
$data = file_get_contents('test.txt');

// use preg_match_all to parse the data
preg_match_all("/stuffyd\[([0-9]+)\].*\"(.*?)\"/im", $data, $matches);

// you could echo here but let's convert the matches into a usable array
$count=count($matches);
for ($i=0; $i<$count; $i++){
    $items[$matches[1][$i]]=$matches[2][$i];
}

// and then echo out the key and value of each pair
foreach ($items as $id => $name){
    echo $name . ':' . $id . chr(10);
}

http://uk1.php.net/preg_match_all

答案 1 :(得分:0)

$contents = file_get_contents("test.txt");
preg_match_all('/stuffyd\[(\d+)\].*=name:\s*"(.*)"/', $contents, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
    echo $match[2] . ':' . $match[1] . "\n";
}

答案 2 :(得分:0)

@Popnoodles和@Barmar的答案最好使用preg_match_all

但要理解为什么你的代码不起作用,有3个问题。

(1)在function getItemName($i)$itemNameFile爆炸$itemName中的数组看起来像 -

Array
(
    [0] => stuffyd[1] = {name: 
    [1] => Item 1
    [2] => };
stuffyd[2] = {name: 
    [3] => Item 2
    [4] => };
stuffyd[3] = {name: 
    [5] => Item 3
    [6] => };
stuffyd[4] = {name: 
    [7] => Item 4
    [8] => };
stuffyd[5] = {name: 
    [9] => Item 5
    [10] => };
stuffyd[6] = {name: 
    [11] => Item 6
    [12] => };
)

所以你需要做

return $itemName[$i+($i-1)];

(2)在function getNamesID()中你只循环了5次

for($i = 1; $i < 6; $i++)

获取所有6个你需要使用<=6

for($i = 1; $i <= 6; $i++)

(3)function getNamesID() echo ... for()位于您的for()循环之外,因此您只能获得最后一个实例。将echo $itemName1 . ":" . $itemID1 . chr(10); } // end for() } // end function 循环结束括号移到echo之后 -

<?php
function getItemName($i) {
    $itemNameFile = file_get_contents('test.txt');
    $itemName = explode('"', $itemNameFile);
    return $itemName[$i+($i-1)];
}
function getItemID($i) {
    $itemIDFile = file_get_contents('test.txt');
    $itemID1 = explode('stuffyd[', $itemIDFile);
    $itemID = explode(']', $itemID1[$i]);
    return $itemID[0];
}
function getNamesID() {
    for($i = 1; $i <= 6; $i++) {
        $itemName1 = getItemName($i);
        $itemID1 = getItemID($i);
        echo $itemName1 . ":" . $itemID1 . chr(10);
    }
}
getNamesID();
?>

所以现在你的代码看起来像 -

{{1}}