我试图将条目ID和文件名与preg_match_all匹配,但无法获得第二个。
<div class="flip-entry" id="entry-8F_mU7gcLkLVGN2Rpb3FyX10JVEZ" tabindex="0" aria-label="Name: File_Name.txt. Drücken Sie zum Öffnen die Eingabetaste.">
我只想获得8F_mU7gcLkLVGN2Rpb3FyX10JVEZ
和File_Name.txt
。
答案 0 :(得分:1)
<?php
$str='<div class="flip-entry" id="entry-8F_mU7gcLkLVGN2Rpb3FyX10JVEZ" tabindex="0" aria-label="Name: File_Name.txt. Drücken Sie zum Öffnen die Eingabetaste.">';
$dom = new DOMDocument;
$dom->loadHTML($str);
foreach ($dom->getElementsByTagName('div') as $tag) {
$entryid = $tag->getAttribute('id');
$fname = $tag->getAttribute('aria-label');
}
echo explode('-',$entryid)[1];
echo rtrim(explode(' ',$fname)[3],'.');
<强> OUTPUT :
强>
8F_mU7gcLkLVGN2Rpb3FyX10JVEZ
File_Name.txt
答案 1 :(得分:0)
$str='<div class="flip-entry" id="entry-8F_mU7gcLkLVGN2Rpb3FyX10JVEZ" tabindex="0" aria-label="Name: File_Name.txt. Drücken Sie zum Öffnen die Eingabetaste.">';
$preg = '/(entry-(.*)\")(\w+\.\w{2,4})/';
preg_match_all($preg, $str);
我认为这应该做......
答案 2 :(得分:0)
感谢Shankar Damodaran我将代码修改为:
function parseHTML($url) {
$page = getPage($url);
$dom = new DOMDocument;
$dom->loadHTML($page);
$i = 0;
foreach($dom->getElementsByTagName('div') as $tag) {
$entryid = $tag->getAttribute('id');
if(stripos($entryid, 'entry') !== false) {
$items[$i]['id'] = explode('-',$entryid)[1];
$filename = $tag->getAttribute('aria-label');
$items[$i]['name'] = cut_str($filename, 'Name: ', '. ');
$i++;
}
}
return $items;
}