Page 1 of 1

using substr with file listing

Posted: Fri May 21, 2010 5:35 pm
by mickyjune26
Hey all,
I'm really starting to like and appreciate this forum.

I've learned how to list files in a directory, but would like to limit the length of the file name as displayed on the page to the first 20 characters, replacing the remaining with "...".

I know substr can do this, but I'm confused on how to insert it into the code below. Would I place .$file. in the substr function?

Thanks.

Code: Select all

<?php
 if ($handle = opendir('./content/Workspace Lessons/Workspace Lessons/Language Arts')) {
   while (false !== ($file = readdir($handle)))
      {
          if ($file != "." && $file != "..")
	  {
          	$thelist .= '<a class="body_css" href="/content/Workspace Lessons/Workspace Lessons/Language Arts/'.$file.'">'.$file.'</a><br/>';
          }
       }
  closedir($handle);
  }
?>
                <P>
                  <?=$thelist?>
                </p>
                <?php
unset($thelist);
?>

Re: using substr with file listing

Posted: Sat May 22, 2010 5:39 am
by social_experiment

Code: Select all

<?php '<a class="body_css" href="/contents/Workspace Lessons/Workspace Lessons/Lanuage Arts/'.$file.'">'.substr($file, 0, 20).'</a>'; ?>
Im sure sure how you would replace the remaining 'missing characters' with '...' unless you are refering to just 3 dots in which case it would be

Code: Select all

<?php '<a class="body_css" href="/contents/Workspace Lessons/Workspace Lessons/Lanuage Arts/'.$file.'">'.substr($file, 0, 20).'...</a>'; ?>

Re: using substr with file listing

Posted: Sat May 22, 2010 11:03 pm
by Chalks
social_experiment wrote:

Code: Select all

<?php '<a class="body_css" href="/contents/Workspace Lessons/Workspace Lessons/Lanuage Arts/'.$file.'">'.substr($file, 0, 20).'</a>'; ?>
Im sure sure how you would replace the remaining 'missing characters' with '...' unless you are refering to just 3 dots in which case it would be

Code: Select all

<?php '<a class="body_css" href="/contents/Workspace Lessons/Workspace Lessons/Lanuage Arts/'.$file.'">'.substr($file, 0, 20).'...</a>'; ?>
I think he means the latter. Also, you have to test and make sure the file is longer than 20 characters long, otherwise you would get something like:

file1.php...
file2.php...
thisfileisareallylon...

so, I would do this:

Code: Select all

// without worrying about file extensions:
if(strlen($file>20))
  echo substr($file, 0, 20) . " ...";

// worrying about file extensions:
if(strlen($file>24))
  echo substr($file, 0, 20) . " ... " . substr($file, -4, 4);

Re: using substr with file listing

Posted: Mon May 24, 2010 7:55 am
by mickyjune26
Got it. I'm almost there. I'm having trouble incorporating the if statement. In the form below, it is only applying my "..." if statement to the first file that is more than 5 characters.
http://www.einstructiontx.com/content_workspace.php

Code: Select all

<?php
 if ($handle = opendir('./content/Workspace Lessons/Workspace Lessons/Math')) {
   while (false !== ($file = readdir($handle)))
      {
          if ($file != "." && $file != ".." && strlen($file>5))
	  {
          	$thelist .= '<a class="body_css" href="/content/Workspace Lessons/Workspace Lessons/Math/'.$file.'">- '.substr($file, 0, 5).'...</a><br/>';
          }
		  else 
		  {
          	$thelist .= '<a class="body_css" href="/content/Workspace Lessons/Workspace Lessons/Math/'.$file.'">- '.$file.'</a><br/>';
          }
       }
  closedir($handle);
  }
?>
[syntax]

Re: using substr with file listing

Posted: Mon May 24, 2010 9:08 am
by Chalks

Code: Select all

if ($file != "." && $file != ".." && strlen($file>5))
You've got the parentheses in the wrong place. It should be this:

Code: Select all

if ($file != "." && $file != ".." && strlen($file)>5)

Edit: Oh, you also shouldn't combine the "." and ".." test with the strlen test:

Code: Select all

if ($file != "." && $file != "..") {
    if(strlen($file)>5)
      //print with dots
    else
      //print without dots
}

Re: using substr with file listing

Posted: Mon May 24, 2010 9:20 am
by mickyjune26
thanks man. I'll have to spend more time on php.net, looking at syntax. I'm currently watching training videos on lynda.com to get a good foundation.

Re: using substr with file listing

Posted: Mon May 24, 2010 11:06 am
by Chalks
the way I got started was actually with this tutorial. It might be a little basic for you at this point, but it definitely goes over all the important bits.

Re: using substr with file listing

Posted: Mon May 24, 2010 11:21 am
by mickyjune26
thanks for the tip. It's always good to be directed to a good tutorial.

Re: using substr with file listing

Posted: Mon May 24, 2010 11:22 am
by mickyjune26
btw - here is the finished product. It incorporates HTML "Title" so it shows the full filename if someone hovers over the link.

http://www.einstructiontx.com/content_workspace.php