javascript - Trying to create a menu dynamically with php using preg_match_all to my server php documents -
the project consists of lateral menu, contains blog entries. in theory, menu works way: <h1>
title of each of blog documents , corresponding href
, using php.
so each time add new blog entry, appears in menu automatically, did using fopen , fread , saving contents of each php document of blog in variable called $leer.
then, used preg_match_all search title of each blog entry , display on menu. can make menu without having add links manually, using scandir url.
the problem that, when using preg_match_all in array, gives me many incorrect results, obtaining same <h1>
title 4 times without explanation. here code:
$escanear = scandir ("/home/sites/thetoptenweb.com/public_html/post"); $tamanoarray = count($escanear);
so first, can see, i´m using scandir , count number of pages.
for($i=2;$i<=$tamanoarray-3;$i++){ $abrirfichero = fopen($escanear[$i],"r"); $leer=fread($abrirfichero, filesize($escanear[$i])); fclose($abrirfichero); }
then, use for loop , fread read documents. loop made "scan" selected files between second , last-3, because blog entries.
preg_match_all('%<h1>(.*)</h1>%', $leer, $arraymaches);
so, preg_match_all
function title of documents in multi-dimensional array, that array giving me problems i think, because tryed read each result foreach
loops, there blank results , don´t know why happens.
foreach ($arraymaches $result) { foreach ($result $key) { } } $cortado=preg_split('%</?h1>%', $key); echo "<a href=".'"'.$escanear[$i].'"'."><li>".$cortado[0]."</li></a><hr>";
finally, used foreach loops access multi-dimensional array of preg_match_all. then, preg_splited results text without html tags , after that, displayed results echo.
hope helps me recognizing problem and, if possible, explanation preg_match_all array because don´t understand how it´s created. suggestions admited. if know better way this, i´ll happy read it. entire code:
<?php $escanear = scandir ("/home/sites/thetoptenweb.com/public_html/post"); $tamanoarray = count($escanear); for($i=2;$i<=$tamanoarray-3;$i++){ $abrirfichero = fopen($escanear[$i],"r"); $leer=fread($abrirfichero, filesize($escanear[$i])); fclose($abrirfichero); preg_match_all('%<h1>(.*)</h1>%', $leer, $arraymaches); foreach ($arraymaches $result) { foreach ($result $key) { } } $cortado=preg_split('%</?h1>%', $key); echo "<a href=".'"'.$escanear[$i].'"'."><li>".$cortado[0]."</li></a><hr>"; } ?>
thanks.
just use dom... you'll save trouble.
$menudata = array(); $iter = new directoryiterator('/home/sites/thetoptenweb.com/public_html/post'); foreach ($iter $file) { if ($file->isfile()) { $filename = $file->getfilename(); $path = $file->getpathname(); $dom = new domdocument(); $dom->loadhtmlfile($path); $titlenode = $dom->getelementsbytagname('h1')->item(0); if ($titlenode) { $title = $titlenode->nodevalue; $menudata[$filename] = $title; } } }
now have stuff in $menudata
can loop on , output links, assuming filename appropriate url. alternatively, output links in loop directly it's wiser separate things. create function data need, , use data output.
but better solution pick blog platform , use that, spend time writing importer , adjusting , feel suit.
Comments
Post a Comment