简体   繁体   English

PHP-如何获取不同类名的span值

[英]PHP - How to get value of span with different class name

I'm trying to get the values of two span with different class name and put it into array 我正在尝试使用不同的类名获取两个span的值并将其放入数组中

this is the html 这是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>'

as for now i have this code 至于现在我有这个代码

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]); 

i don't know how to put this in array or if you have much better coeds to put this in array. 我不知道如何将其放入数组中,或者您是否有更好的方法将其放入数组中。

result must be 结果必须是

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

I'm not a regex master, but I've modified it to strip the : sign: 我不是正则表达式大师,但我对其进行了修改以去除:号:

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);
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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM