سؤال

I' trying to match entry ID and file name with preg_match_all but can't get the second one.

<div class="flip-entry" id="entry-8F_mU7gcLkLVGN2Rpb3FyX10JVEZ" tabindex="0" aria-label="Name: File_Name.txt. Drücken Sie zum Öffnen die Eingabetaste.">

I want to get 8F_mU7gcLkLVGN2Rpb3FyX10JVEZ and File_Name.txt only.

هل كانت مفيدة؟

المحلول 3

Thanks to Shankar Damodaran I modified the code to this:

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

نصائح أخرى

This works for the current scenario.. Not an ideal solution though....

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

I think this should do...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top