2016-07-13 12 views
-1

Ich möchte Informationen aus einem HTML-Code extrahieren. Hier ist ein Teil davon:Extrahieren Sie einige Informationen aus HTML-Code

<li class="letter">#</li> 
<li><a href="/fr/707/mot1/1.html" title="mot2">mot2</a><span>1234</span></li>  
<li><a href="/fr/1042/mot3/1.html" title="mot4">mot4</a><span>4567</span></li> 
<li><a href="/fr/5697/mot5/1.html" title="mot6">mot6</a><span>3254</span></li>etc... 

dieses Ergebnis zu erhalten:

mot1,mot2 
mot3,mot4 
mot5,mot6 
etc... 

Ich habe versucht, mit strip_tags aber das macht mich nicht weit ...

Vielen Dank für Ihre Hilfe.

+0

Verwendung der HTML-Analyse https://davidwalsh.name/php-notifications – developerCK

+0

Sie sollten in der Lage zu verwenden [DomDocument] (http://php.net/manual/en /class.domdocument.php), um dies zu handhaben - ohne Ihren HTML-Code zu sehen, ist es schwierig, genauer zu sein. –

+0

Sorry, aber ich musste. Antwort mit geringem Aufwand, Antwort mit geringem Aufwand: http://pastie.org/10906079;) –

Antwort

0

Sie könnten versuchen, eine Mischung aus preg_split, preg_match, array und foreach Schleife zu bekommen, was Sie sich wünschen. In diesem Fall; Sie können einfach alle mot Worte zu einem array wie so bündeln:

<?php  
    $strMots = '<li class="letter">#</li> 
    <li><a href="/fr/707/mot/1.html" title="mot2">mot2</a><span>1234</span></li> 
    <li><a href="/fr/707/mot1/1.html" title="mot2">mot2</a><span>1234</span></li> 
    <li><a href="/fr/1042/mot3/1.html" title="mot4">mot4</a><span>4567</span></li> 
    <li><a href="/fr/5697/mot5/1.html" title="mot6">mot6</a><span>3254</span></li>'; 

    // SPLIT THE STRING $strMots AT THE BOUNDARY "</li>" 
    $arrSplits = preg_split("#<\/li>#", $strMots); 

    // CREATE AN ARRAY TO HOLD YOUR mot STRINGS 
    $arrMots = array(); 

    // USING FOREACH LOOP; ITERATE THROUGH ALL THE $arrSplits 
    // WHILE YOU ARE AT IT, JUST CHECK IF EACH RECORD IN THE LOOP MATCHES 
    // A PATTERN THAT HAS THE WORD mot IN IT AND IF IT DOES, 
    // PUSH IT TO AN ARRAY... 
    foreach($arrSplits as $split){ 
     if(preg_match_all("#mot[0-9]+#si", $split, $matches)){ 
      if(!empty($matches)){ 
       foreach($matches as $match){ 
        if(is_array($match)){ 
         foreach($match as $motVal){ 
          if(!in_array($motVal, $arrMots)){ 
           $arrMots[] = $motVal; 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
    var_dump($arrMots); 

    // DISPLAYS: 
    array (size=4) 
     0 => string 'mot2' (length=4) 
     1 => string 'mot1' (length=4) 
     2 => string 'mot3' (length=4) 
     3 => string 'mot4' (length=4) 
     4 => string 'mot5' (length=4) 
     5 => string 'mot6' (length=4) 

HINWEIS: dass sein Bitte informiert (wie confirmable vom var_dump() oben, die Regular Expression hier verwendet wird, nicht nur mot entsprechen würde . allein das heißt: ohne Nummer-Suffix wie mot9

Testen sie es aus. HERE.

Hoffe das gibt dir ein paar Ideen, wie du selbst improvisieren kannst.

Good Luck & Prost ;-)

Verwandte Themen