匹配ID和文件名与preg_match_all

时间:2014-03-19 09:25:08

标签: php preg-match-all

我试图将条目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_mU7gcLkLVGN2Rpb3FyX10JVEZFile_Name.txt

3 个答案:

答案 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;
}