Again about list files with description

Today I managed to refine the code, got with your help.
This is now my working (you can see it working here) but still with something to improve, code:

<?php 
declare(strict_types=1); // this file wide type checking
//error_reporting(-1); // display maximum errors
// ini_set('display_errors', 'true'); // show errors on screen

?>

<h2>Le pagine di questa sezione</h2>
<?php
# SET PATH
  $PATH  = './[^index]*.php';

# GET RESULTS
  $result = ''; // return a string of all files containing 'description'
  $aTmp   = glob($PATH); // receive every file in the $path directory
  foreach( $aTmp as $key => $file )
  {
    $sLowercase = strtolower($file) ;
    if('index.php'===$sLowercase)
    {
      echo '<h2> Do not use: '  .$file .'</h2>' ;

    }else{
      if( strpos($sLowercase, '.php') ) 
      {
        $hasDescription = getRows($file);
        if( strlen( $hasDescription ) )
        {
          # CONCATENATE $hasDescription;
          $result .= $hasDescription;
        }
      }//endif strpos(...);
    }//endif('index.php'===$sLowercase)
 }//endforeach

# RENDER RESULTS
  if( strlen($result) )
  { 
    echo showResults($PATH, $result);

  }else{ // must be empty
    echo '<h2> No PHP files containing a meta [description] </h2>';
  }// endif

echo '</body></html>';


// ONLY FUNCTIONS BELOW

//========================================
function getRidOfEqualSign( string $tmp)
: string 
{
  $result = ''; // EMPTY IF FALSE

  $iPos = strpos($tmp, '=');
  $iPos = strpos($tmp, '"');
  $result = substr($tmp, ++$iPos);

  return $result;
}//


//======================================
function getRows( string $file)
: string 
{
  $result = ''; // eEMPTY IF FALSE

  $contents      = file_get_contents($file);

  $ititle        = (int) strpos( $contents , "\$title");
  $isubtitle     = (int) strpos( $contents , "\$subtitle");
  $idescription  = (int) strpos( $contents , "\$description");
  $ikeywords     = (int) strpos( $contents , "\$keywords");

  if( $ititle || $isubtitle || $idescription || $ikeywords)
  {
    # GET FILE NAME ONLY
      $fileName = substr(strrchr($file, '/'), 1) ;            

    # TITLE
      $title = '';
      if($ititle) {
        $title = getValue($contents, $ititle);
      }


      # SUBTITLE
      $subtitle = '';
      if($isubtitle) {
        $subtitle = getValue($contents, $isubtitle);
      }


    # DESCRIPTION 
      $desc = '';
      if($idescription) {
        $desc = getValue($contents, $idescription);
      }

    # KEYWORDS
      $keywords = '';
      if($ikeywords) {
        $keywords = getValue($contents, $ikeywords);
      }

      $BDR = 'style="border: solid 1px #ddd; border-collapse: collapse;"';

      $result = <<< ____EOT
        <li>
          <b> <a href="$file"> $title</a></b>, o <i>$subtitle</i>:\n
           $desc
        </li>
____EOT;

  }// endif;

  return $result;
}//


//======================================================
function showResults( string $PATH, string $rows )
: string 
{

  $result = <<< ____EOT
    <ul>
      $rows
    </ul>
____EOT;

  return $result;
}//


//=====================================================
function getValue(string $contents, int $iStart)
: string 
{
  $result = '';

  $result   = substr($contents, ++$iStart) ;
  $iFinish  = strpos($result, ";");
  $value    = substr($contents, $iStart, --$iFinish) ;
  $result   = getRidOfEqualSign($value); 

  return $result;
}

?>

The order of files is not the best: could I order the listed files with a new metatag (maybe bad from a w3c validator point of view), something link <meta name="order" content="1" />?

You would probably do better refining your getRows function to return an array of objects that you could then sort.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.