0

我正在尝试获取具有不同类名的两个跨度的值并将其放入数组中

这是html

$html = '<div class="members">
            <span class="records">Name: </span>
            <span class="values">Marco</span>
        </div>
        <div class="members">
            <span class="records">Mobile: </span>
            <span class="values">+9431109890</span>
        </div>
        <div class="members">
            <span class="records">Age: </span>
            <span class="values">33</span>
        </div>
        <div class="members">
            <span class="records">Sex: </span>
            <span class="values">Male</span>
        </div>'

至于现在我有这个代码

preg_match_all("/\<span class\=\"records\"\>(.*?)\<\/span\>/", $html, $records);
preg_match_all("/\<span class\=\"values\"\>(.*?)\<\/span\>/", $html, $values);
$valueresult = implode("\n", $records[1]); 
$recordresult = implode("\n", $values[1]); 

我不知道如何将其放入数组中,或者您是否有更好的 coeds 将其放入数组中。

结果必须是

  array(
    'Name'  => 'Marco',
    'Mobile' => '+000000000',
    'Age' => '33',
    'Sex' => 'Male,
  )
4

2 回答 2

0
preg_match_all("/\<span class\=\"records\"\>(.*?)\<\/span\>/", $html, $records);
preg_match_all("/\<span class\=\"values\"\>(.*?)\<\/span\>/", $html, $values);

$recordresult = $values[1];
$valueresult = $records[1];

$result  = array_combine($valueresult, $recordresult);
var_dump($result);
于 2013-07-01T11:21:57.337 回答
0

我不是正则表达式大师,但我已对其进行了修改以去除:符号:

preg_match_all("/\<span class\=\"records\"\>(.*?):(.*?)\<\/span\>/", $html, $records);
preg_match_all("/\<span class\=\"values\"\>(.*?)\<\/span\>/", $html, $values);

$combined = array_combine($records[1], $values[1]);

print_r($combined);
于 2013-07-01T11:15:25.183 回答