Friday, 23 June 2017

javascript - Trying to create a menu dynamically with php using preg_match_all to my server php documents

The project consists of a lateral menu, which contains all my blog entries.
In theory, the menu works this way: I get the

title of each of my blog documents and his corresponding href, using php.






So each time i add a new blog entry, it appears in the menu automatically,
i did this using fopen and fread and saving the contents of each php document of my blog in a variable called $leer.







Then, i used Preg_match_all to search the title of each blog entry and display it on the menu. With this I can make the menu without having to add the links manually, also using scandir get the url.






The problem is that, when using preg_match_all in the array, gives me many incorrect results, obtaining the same

title four times without explanation.
Here is the code:



$escanear = scandir ("/home/sites/thetoptenweb.com/public_html/post");
$tamanoArray = count($escanear);



So first, as you can see, i´m using scandir and count to get the number of pages.



for($i=2;$i<=$tamanoArray-3;$i++){
$abrirFichero = fopen($escanear[$i],"r");
$leer=fread($abrirFichero, filesize($escanear[$i]));
fclose($abrirFichero);
}



Then, i use a for loop and fread to read all my documents.
The loop is made to "scan" only the selected files between the second and the last-3, because those are my blog entries.



preg_match_all('%

(.*)

%', $leer, $arrayMaches);


So, with the preg_match_all function i get the Title of my documents in a multi-dimensional array, that array is giving me problems i think, because i tryed to read each result with foreach loops, but there are some blank results and i don´t know why this happens.



foreach ($arrayMaches as $result) {
foreach ($result as $key) {

}
}

$cortado=preg_split('%%', $key);

echo "
  • ".$cortado[0]."

  • ";


    Finally, i used foreach loops to access to the multi-dimensional array of the preg_match_all. Then, i preg_splited the results to get the text without html tags and after that, displayed the results with echo.




    Hope someone helps me recognizing the problem and, if possible, an explanation to the preg_match_all array because i don´t understand how it´s created. Suggestions admited.
    If you know a better way to do this, i´ll happy to read it.
    This is the entire code:



        $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('%

    (.*)

    %', $leer, $arrayMaches);
    foreach ($arrayMaches as $result) {
    foreach ($result as $key) {
    }
    }
    $cortado=preg_split('%%', $key);
    echo "
  • ".$cortado[0]."

  • ";
    }
    ?>



    Thanks.

    No comments:

    Post a Comment

    casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

    In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...