List and link php files in a folder and its metatag description

But, when you access the php code via the filesystem, and not via the server, the include files are not processed because it is only reading your PHP source code, not executing it.

You can call the metatags script on a remote server; you can’t GLOB a remote server.

1 Like

Today I will try to add links to the names of files. Thank you.

John, today I managed to link the listed files, but the result is quite ugly.
This is the modified code

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

$contents = file_get_contents($file);
$iStart   = strpos( $contents , "\$description");
if( $iStart ) 
{
 $tmp    = substr($contents, ++$iStart) ;
 $tmp2   = strpos($tmp, ';');

 $result = '<ul>'
         . '<li><b><a href="'.$file.'">$file</a></b>: ' //$file 
         .   substr($contents, $iStart, $tmp2)
         .'</li></ul>'
         ;
}// endif;

return $result;
}//

How could I get only the *name of the file (without extension) : *description (without word “description” before).
Thanks a lot for your excellent work.

1 Like

Try this:

https://www.php.net/manual/en/function.strrchr.php

Search for “/“, get the position and use substr(…)

This is only one solution, I should imagine there are many more.

Edit

To remove the .PHP file extension try str_replace(…) or I think strstr(…)

1 Like

Thank you. Today I managed to list and link the php files with only its name. But there is still a description= which I’d rather avoid.

The code I used (just the modified part)

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

  $contents = file_get_contents($file);
  $iStart   = strpos( $contents , "\$description");
  if( $iStart ) 
  {
    $tmp    = substr($contents, ++$iStart) ;
    $tmp2   = strpos($tmp, ';');

    $result = '<ul>'
            . '<li><b><a href="'.$file.'">'
            .   substr("$file",2,-4)
            . '</a></b>: ' //$file 
            .   substr($contents, $iStart, $tmp2)
            .'</li></ul>'
            ;
  }// endif;

  return $result;
}//

If you just want the text removed after description = then try the following function:

<?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


$aDescriptions = [
  'description="come estrarre exif da immagini con php" ',
  'description = $aTags("description"] ?? FALSE',
  'description="ricavare metatags"',
];

foreach($aDescriptions as $key => $desc) :
  echo '<br>ORIGINAL ==> ' . $desc;
  echo '<br>FILTERED ==> ' . getRidOfEqualSign($desc);
  echo '<br>';
endforeach;


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

  $iPos = strpos($tmp, '=');

  $result = substr($tmp, ++$iPos); // notice the preceeding ++ 

  return $result;
}//

Please note that PHP has numerous functions that are very helpful and it is worth glancing at all the functions details for future reference,

https://www.php.net/manual/en/ref.strings.php

1 Like

No, I just want to remove the word “description =”. More simply :slight_smile:

Try the following:

if( $iStart ) 
  {
    $tmp    = substr($contents, ++$iStart) ;
    $tmp2   = strpos($tmp, ';');
    $tmp3 = substr($contents, $iStart, $tmp2) ;
    $tmp3 = getRidOfEqualSign($tmp3); // make sure the function is available

    $result = '<ul>'
            . '<li><b><a href="'.$file.'">'
            .   substr("$file",2,-4)
            . '</a></b>: ' //$file 
            .    $tmp3
            .'</li></ul>'
            ;
  }// endif;

Not tried because I’m very late for lunch :slight_smile:

1 Like

Excuse me for this my late answer, but your last code unfortunately doesn’t work (even changing the second $tmp3 to $tmp4) …

I made the modifications to the previous Post #49 and this works OK:

<?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


echo '$PATH ==> ',
  $PATH = '/var/www/john-betong.tk/public_html/'
        . '*.*';

$result = ''; // return a string of all files containing 'description'

$aTmp = glob($PATH); // receive every file in the $path directory
// vd($aTmp);

  foreach( $aTmp as $key => $file )
  {
    
    $sLowercase = strtolower($file) ;
    if('index.php'===$sLowercase)
    {
      echo '<h2> Do not use: '  .$$file .'</h2>' ;

    }else{
      if( strpos($sLowercase, '.php') ) 
      {
        // echo '<br>' .$file;;
        $aTags  = get_meta_tags($file);

        // Prevent index not found error
        $description = $aTags['description'] ?? FALSE ;
        if($description) 
        {
          $result .=  '<dl>';
         #$result .=  '<dt> <b> FILE [meta]: </b> ' .$key .'</dt>';
          $result .=   '<dd> &lt;meta&gt; description = "' .$description .'"&gt; </dd>';
          $result .=  '</dl>';
        }//endif($description) 
        $hasDescription = getDescription($file);
        if( strlen( $hasDescription ) )
        {
          $result .= $hasDescription;
        }
      }//endif strpos(...);  

    }//endif('index.php'===$sLowercase)

 }//endforeach

if( strlen($result) )
{ 
  echo $result;
}else{ // must be empty
  echo '<h2> There are no PHP files with containing a meta [description] </h2>';
}// endif

echo '<h3> Finished processing </h3> ';


// ONLY FUNCTIONS BELOW

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

  $contents = file_get_contents($file);
  $iStart   = strpos( $contents , "\$description");

  if( $iStart ) 
  {
    $tmp  = substr($contents, ++$iStart) ;
    $tmp2 = strpos($tmp, ';');
    $tmp3 = substr($contents, $iStart, $tmp2) ;
    $tmp4 = getRidOfEqualSign($tmp3); // make sure the function is available

    $result = '<ul>'
            .   '<li> ORIGINAL ==> <br> &lt;meta ' .$tmp3 .'&gt; <br><br> </li>'
            .   '<li><b>'
            .       '<a href="'.$file.'">'
            .           substr("$file", 0, -4)
            .         '</a></b> ==> <br>' //$file 
            .    $tmp4
            .'</li>'
            .'</ul><br><br>'
            ;
  }// endif;

  return $result;
}//


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

  $iPos = strpos($tmp, '=');

  $result = substr($tmp, ++$iPos);

  return $result;
}//

//======================================
function vd( $val, $vd=FALSE )
{
  echo '<pre class="w88 mga"> <br><br><hr>';
  if($vd):
    print_r($val);
  else:
    var_dump($vd);  
  endif;  
  echo '</pre>';
}//  
1 Like

Good! Today I managed to use your code (with some modifications) to get a list of files with their description.
There would be some further improvements, if you want and I don’t ask too much :slight_smile: .
That is: 1) avoid to list index.php (necessary), 2) add, besides description metatag, also title and author metatag (possible?).

Please post your modifications.

1) avoid to list index.php (necessary)

Check the script because it looks as though checks are already in place:

$sLowercase = strtolower($file) ;
    if('index.php'===$sLowercase)
    {
      echo '<h2> Do not use: '  .$file .'</h2>' ;
    }else{ 

2) add, besides description metatag, also title and author metatag (possible?).

I think you would need to add two more checks for title and author instead of just description.

  // Prevent index not found error
        $description = $aTags['description'] ?? FALSE ;
        if($description) 
        {
          $result .=  '<dl>';
         #$result .=  '<dt> <b> FILE [meta]: </b> ' .$key .'</dt>';
          $result .=   '<dd> &lt;meta&gt; description = "' .$description .'"&gt; </dd>';
          $result .=  '</dl>';
        }//endif($description) 
        $hasDescription = getDescription($file);
        if( strlen( $hasDescription ) )
        {
          $result .= $hasDescription;
        }
1 Like

Sorry for my delay. Today I managed to work to this code, and this is what I have now:

<?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
?>


<?php 
$keywords="";
$title="estrarre le descrizioni";
$autore="Bertoldi";
$description="estrarre le descrizioni in modo articolato";
$img="http://localhost/img/ritratti/someone.jpg";
include "normal.inc";
include "$root/intell/header-intell.inc";

?>

<?php 
/*echo '$PATH ==> ',*/
        
$PATH = './'
        . '*.*';        

$result = ''; // return a string of all files containing 'description'

$aTmp = glob($PATH); // receive every file in the $path directory
// vd($aTmp);

  foreach( $aTmp as $key => $file )
  {
    
    $sLowercase = strtolower($file) ;
    if('index.php'===$sLowercase)
    {
      echo '<h2> Do not use: '  .$$file .'</h2>' ;

    }else{
      if( strpos($sLowercase, '.php') ) 
      {
        // echo '<br>' .$file;;
        $aTags  = get_meta_tags($file);

        // Prevent index not found error
        $description = $aTags['description'] ?? FALSE ;
        $title = $aTags['title'] ?? FALSE ;
        $autore = $aTags['autore'] ?? FALSE ;
        if($description) 
        {
          $result .=  '<dl>';
         #$result .=  '<dt> <b> FILE [meta]: </b> ' .$key .'</dt>';
          $result .=   '<dd> &lt;meta&gt; description = "' .$description .'"&gt; </dd>';
          $result .=  '</dl>';
        }//endif($description) 
        $hasDescription = getDescription($file);
        if( strlen( $hasDescription ) )
        {
          $result .= $hasDescription;
        }
      }//endif strpos(...);  

    }//endif('index.php'===$sLowercase)

 }//endforeach

if( strlen($result) )
{ 
  echo $result;
}else{ // must be empty
  echo '<h2> There are no PHP files with containing a meta [description] </h2>';
}// endif

//echo '<h3> Finished processing </h3> ';


// ONLY FUNCTIONS BELOW

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

  $contents = file_get_contents($file);
  $iautore  = strpos( $contents , "\$autore");  
  $ititle   = strpos( $contents , "\$title");
  $idescription  = strpos( $contents , "\$description");

  if( $idescription ) 
  {
    $tmp  = substr($contents, ++$idescription) ;
    $tmp2 = strpos($tmp, ';');
    $tmp3 = substr($contents, $idescription, $tmp2) ;
    $tmp4 = getRidOfEqualSign($tmp3); // make sure the function is available

    $result = '<ul>'
            .   '<li><b>'
            .       '<a href="'.$file.'">'
            .           substr("$file",2,-4)            
            .         '</a></b>: ' //$file 
            .    $iautore
            .    ', <b>'            
            .    $ititle 
            .    '</b>'            
            .    ' ('
            .    $tmp4
            .    ')'            
            .'</li>'
            .'</ul>'
            ;
  }// endif;

  return $result;
}//

//echo "$autore $title";

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

  $iPos = strpos($tmp, '=');

  $result = substr($tmp, ++$iPos);

  return $result;
}//
?>

But the result is that I get a number instead of a text for $autore and $title variables, as you can see in the attached following image:

What should I change?

Thank you!

The original question was to find the value of METATAGS and not find PHP variables. If the latter is now the requirement then please raise another Topic.

Please also ensure that the $PATH variable includes files with either a .php and/or .html extension.

Also try this modification which I have tested and it works fine:

<?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
?>


<?php 
/*
$keywords="";
$title="estrarre le descrizioni";
$autore="Bertoldi";
$description="estrarre le descrizioni in modo articolato";
$img="http://localhost/img/ritratti/someone.jpg";
# include "normal.inc";
# include "$root/intell/header-intell.inc";
*/
?>

<?php 
/*echo '$PATH ==> ',*/
      
$PATH = '/var/www/icanpostit.com/public_html/'
      . '*.*';

if(0) :
  $PATH = './'
        . '*.*';        
endif;        
echo '$PATH ==> ', $PATH .'<br>';

$result = ''; // return a string of all files containing 'description'

$aTmp = glob($PATH); // receive every file in the $path directory
// vd($aTmp);

  foreach( $aTmp as $key => $file )
  {
    $lowercase = strtolower($file) ;
    if('index.php'===$lowercase)
    {
      echo '<h2> Do not use: '  .$file .'</h2>' ;
    }else{
      if( strpos($lowercase, '.php') || strpos($lowercase, '.html')) 
      {
        $aTags  = get_meta_tags($file);
        if( 1 && count($aTags) ) :
          echo '<dl style="border:dotted 3px RED">DEBUG';
          echo '<dt> File ==> <b>' .$file .'</b> </dt>';
          echo '<dd> <pre> $aTags ==> ';
                print_r($aTags);
          echo '</dd></dl>';
        endif;

        // Prevent index not found error
        $description  = $aTags['description'] ?? FALSE ;
        $title        = $aTags['title']       ?? FALSE ;
        $autore       = $aTags['autore']      ?? FALSE ;
        if($description) 
        {
          $result .=  '<dl>';
          $result .=  '<dt> <b> FILE ==> </b> ' .$file .'</dt>';
          $result .=  '<dd> &lt;meta&gt; description = "' 
                  .     $description .'"&gt;'
                  .   '</dd>';
          $result .=  '</dl>';
          # print_r($result);

        }//endif($description) 
        $hasDescription = getDescription($file);
        if( strlen( $hasDescription ) )
        {
          $result .= $hasDescription;
        }
      }//endif strpos(...);  

    }//endif('index.php'===$lowercase)

 }//endforeach

if( strlen($result) )
{ 
  echo $result;
}else{ // must be empty
  echo '<h2> There are no PHP files with containing a meta [description] </h2>';
}// endif

//echo '<h3> Finished processing </h3> ';


// ONLY FUNCTIONS BELOW

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

  $contents = file_get_contents($file);
  $iautore  = strpos( $contents , "\$autore");  
  $ititle   = strpos( $contents , "\$title");
  $idescription  = strpos( $contents , "\$description");

  if( $idescription ) 
  {
    $tmp  = substr($contents, ++$idescription) ;
    $tmp2 = strpos($tmp, ';');
    $tmp3 = substr($contents, $idescription, $tmp2) ;
    $tmp4 = getRidOfEqualSign($tmp3); // make sure the function is available

    $result = '<ul>'
            .   '<li><b>'
            .       '<a href="'.$file.'">'
            .           substr("$file",2,-4)            
            .         '</a></b>: ' //$file 
            .    $iautore
            .    ', <b>'            
            .    $ititle 
            .    '</b>'            
            .    ' ('
            .    $tmp4
            .    ')'            
            .'</li>'
            .'</ul>'
            ;
  }// endif;

  return $result;
}//

//echo "$autore $title";

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

  $iPos = strpos($tmp, '=');

  $result = substr($tmp, ++$iPos);

  return $result;
}//

Screendump:

1 Like

Thank you: it works, but for a strange reason, while title and description tags are shown correctly, the tag author doesn’t result a text (as it is), but a digit, a number: I don’t understand why.

Please supply the header contents and I will try to duplicate the problem,

1 Like

Thank you. My code now (working but not for author metaga is the following

<?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
?>


<?php 
$keywords="";
$title="estrarre le descrizioni";
$autore="Bertoldi";
$description="estrarre le descrizioni in modo articolato";
$img="http://localhost/img/ritratti/someone.jpg";
include "normal.inc";
include "$root/intell/header-intell.inc";

?>

<?php 
/*echo '$PATH ==> ',*/
        
$PATH = './'
        . '*.*';        

$result = ''; // return a string of all files containing 'description'

$aTmp = glob($PATH); // receive every file in the $path directory
// vd($aTmp);

  foreach( $aTmp as $key => $file )
  {
    
    $sLowercase = strtolower($file) ;
    if('index.php'===$sLowercase)
    {
      echo '<h2> Do not use: '  .$$file .'</h2>' ;

    }else{
      if( strpos($sLowercase, '.php') ) 
      {
        // echo '<br>' .$file;;
        $aTags  = get_meta_tags($file);

        // Prevent index not found error
        $description = $aTags['description'] ?? FALSE ;
        $title = $aTags['title'] ?? FALSE ;
        $autore = $aTags['author'] ?? FALSE ;
        if($description) 
        {
          $result .=  '<dl>';
         #$result .=  '<dt> <b> FILE [meta]: </b> ' .$key .'</dt>';
          $result .=   '<dd> &lt;meta&gt; description = "' .$description .'"&gt; </dd>';
          $result .=  '</dl>';
        }//endif($description) 
        $hasDescription = getDescription($file);
        if( strlen( $hasDescription ) )
        {
          $result .= $hasDescription;
        }
      }//endif strpos(...);  

    }//endif('index.php'===$sLowercase)

 }//endforeach

if( strlen($result) )
{ 
  echo $result;
}else{ // must be empty
  echo '<h2> There are no PHP files with containing a meta [description] </h2>';
}// endif

//echo '<h3> Finished processing </h3> ';


// ONLY FUNCTIONS BELOW

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

  $contents = file_get_contents($file);
  $iautore  = strpos( $contents , "\$autore");  
  $ititle   = strpos( $contents , "\$title");
  $idescription  = strpos( $contents , "\$description");

  if( $idescription ) 
  {
    $tmp  = substr($contents, ++$idescription) ;
    $tmp2 = strpos($tmp, ';');
    $tmp3 = substr($contents, $idescription, $tmp2) ;
    $tmp4 = getRidOfEqualSign($tmp3); // make sure the function is available
    
    $tmpt  = substr($contents, ++$ititle) ;
    $tmpt2 = strpos($tmpt, ';');
    $tmpt3 = substr($contents, $ititle, $tmpt2) ;
    $tmpt4 = getRidOfEqualSign($tmpt3); // make sure the function is available

/*  if I add this the code doesn't work any more, because of errors 
    $tmpa  = substr($contents, ++$iautore) ;
    $tmpa2 = strpos($tmpa, ';');
    $tmpa3 = substr($contents, $iautore, $tmpa2) ;
    $tmpa4 = getRidOfEqualSign($tmpa3); // make sure the function is available
*/
    $result = '<ul>'
            .   '<li><b>'
            .       '<a href="'.$file.'">'
//          .           substr("$file",2,-4)            
            .    $tmpt4 
            .         '</a></b>: ' //$file 
            .    ', <b>'            
//            .    $tmpa4 (see above)
            .    '</b>'            
            .    ' ('
            .    $tmp4
            .    ')'            
            .'</li>'
            .'</ul>'
            ;
  }// endif;

  return $result;
}//


// with this code I see digits instead of text for author metatag
/*
$contents = file_get_contents($file);
  $iautore  = strpos( $contents , "\$autore");  
  $ititle   = strpos( $contents , "\$title");
  $idescription  = strpos( $contents , "\$description");

  if( $idescription ) 
  {
    $tmp  = substr($contents, ++$idescription) ;
    $tmp2 = strpos($tmp, ';');
    $tmp3 = substr($contents, $idescription, $tmp2) ;
    $tmp4 = getRidOfEqualSign($tmp3); // make sure the function is available

    $result = '<ul>'
            .   '<li><b>'
            .       '<a href="'.$file.'">'
            .           substr("$file",2,-4)            
            .         '</a></b>: ' //$file 
            .    $iautore
            .    ', <b>'            
            .    $ititle 
            .    '</b>'            
            .    ' ('
            .    $tmp4
            .    ')'            
            .'</li>'
            .'</ul>'
            ;
  }// endif;

  return $result;
}//
*/


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

  $iPos = strpos($tmp, '=');

  $result = substr($tmp, ++$iPos);

  return $result;
}//
?>

It looks as though i was not clear :frowning:

I was like the header contents of the file which the script I supplied is not finding the METATAG “authors”.

1 Like

I suppose you mean metatag “author” (not “authors”), witch is however a common metatag :frowning:
But the most important: what do you mean by “header content”?
The file containing the above script is one of the files whose metatags should be shown.