【问题标题】:php foreach preg_match'd line, get next linesphp foreach preg_match'd 行,获取下一行
【发布时间】:2016-11-22 08:25:02
【问题描述】:

我希望标题是不言自明的。

我想逐行遍历 xml 文件,然后匹配特定的行(从该行获取属性),然后获取该行之后的下 X 行。

我有以下代码,它试图做到这一点,但我似乎无法弄清楚如何获得接下来的 X 行。

$file = 'Electric.xml';
$lines = file($file);//file in to an array

foreach($lines as $line){

    $reads = element_attributes('WINDOW',$line);

    if($reads['class'] == 'Bracelets'){
        print_r($reads);
    }

    if($reads['class'] == 'Handbags'){
        print_r($reads);
    }
}

function element_attributes($element_name, $xml) {
    if ($xml == false) {
        return false;
    }
    // Grab the string of attributes inside an element tag.
    $found = preg_match('#<'.$element_name.
            '\s+([^>]+(?:"|\'))\s?/?>#',
            $xml, $matches);
    if ($found == 1) {
        $attribute_array = array();
        $attribute_string = $matches[1];
        // Match attribute-name attribute-value pairs.
        $found = preg_match_all(
                '#([^\s=]+)\s*=\s*(\'[^<\']*\'|"[^<"]*")#',
                $attribute_string, $matches, PREG_SET_ORDER);
        if ($found != 0) {
            // Create an associative array that matches attribute
            // names to attribute values.
            foreach ($matches as $attribute) {
                $attribute_array[$attribute[1]] =
                        substr($attribute[2], 1, -1);
            }
            return $attribute_array;
        }
    }
    // Attributes either weren't found, or couldn't be extracted
    // by the regular expression.
    return false;
}

【问题讨论】:

    标签: php foreach preg-match-all


    【解决方案1】:

    使用适当的解析器like SimpleXML 来解析文件。那么你的问题就变得微不足道了。 PHP manual contains a tutorial 可帮助您入门。

    在这种情况下,您只需遍历行,检查您要查找的标签的属性,直到找到匹配项。然后,遍历接下来的 # 个元素,将它们保存到一个数组中。
    像这样的:

    $xml = new SimpleXML ("file.xml");
    foreach ($xml->node->element as $element) {
        if ($element->attribute != "match") {
             continue;
        }
    
        // If we get here we want to save the next # lines/elements.
    }
    

    【讨论】:

    • 恐怕不能使用 XML 解析器。这不是格式良好的 XML。
    • @Ke。甚至 DOMdocument 都失败了是不是很糟糕..? :O 在那种情况下,我为你感到难过,祝你好运!
    • 是的,这太糟糕了!我没有创建文件。我也别无选择,我需要数据!
    • 它要么是“正确的解析器”,要么是“SimpleXML”。选择一个。
    【解决方案2】:
    $linesLength = count($lines);
    $XLines = array();
    for($index = 0; $index < $linesLength; $index++){
    
        $reads = element_attributes('WINDOW',$line);
    
        if($reads['class'] == 'Bracelets'){
            print_r($reads);
            $XLines[] = array_slice($array, $index, $X);
            $index += $X;
        }
    
        if($reads['class'] == 'Handbags'){
            print_r($reads);
            $XLines[] = array_slice($array, $index, $X);
            $index += $X;
        }
    }
    

    【讨论】:

    • 嗨,克里斯,这段代码中有很多缺失的变量。 XLines 和 $array 来自哪里?还有 $X?
    • 这段代码只是替换了你的 foreach clouse,你必须定义 $X。 .
    • $XLines 未定义,$array 也未定义
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-11
    • 1970-01-01
    相关资源
    最近更新 更多