PHP - 为每个段落创建一个数组

时间:2014-01-17 02:54:37

标签: php

我在index.php上输出HTML就像这样:

<p>Value 1, Value 2</p>
<p>Value 3, Value 4</p>
<p>Value 5, Value 6</p>

从外部页面,我想阅读每个段落标记并填充一个数组,如:

array (size 3)

    array (size 2)

        "Title" => "Value 1"

        "Field" => "Value 2"

    array (size 2)

        "Title" => "Value 3"

        "Field" => "Value 4"

    array (size 2)

        "Title" => "Value 5"

        "Field" => "Value 6"

这有可能吗?

2 个答案:

答案 0 :(得分:3)

结帐DOMDocument::getElementsByTagName()。例如:

<?php
$html = <<<HTML
  <p>Value 1, Value 2</p>
  <p>Value 3, Value 4</p>
  <p>Value 5, Value 6</p>
HTML;

$DOM = new DOMDocument();
$DOM->loadHTML($html);

$paragraphs = array();
foreach($DOM->getElementsByTagName('p') as $paragraph) {
    $string = $paragraph->nodeValue;
    list($title, $field) = explode(', ', $string, 2);

    $paragraphs[] = array(
        'title' => $title,
        'field' => $field,
    );
}

var_dump($paragraphs);

输出:

array(3) {
  [0]=>
  array(2) {
    ["title"]=>
    string(7) "Value 1"
    ["field"]=>
    string(7) "Value 2"
  }
  [1]=>
  array(2) {
    ["title"]=>
    string(7) "Value 3"
    ["field"]=>
    string(7) "Value 4"
  }
  [2]=>
  array(2) {
    ["title"]=>
    string(7) "Value 5"
    ["field"]=>
    string(7) "Value 6"
  }
}

答案 1 :(得分:0)

我会使用HTML解析器。我最熟悉的是Simple HTML DOM

$html=file_get_html("file.html");
$paragraphs=new array();
foreach($html->find("p") as $p){
    $text=$p->plaintext();
    $arr=explode(", ",$text);
    $newarr=array("Title"=>$arr[0], "Value"=>$arr[1]);
    array_push($paragraphs,$newarr);
}