PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
mickyjune26
Forum Commoner
Posts: 30 Joined: Mon May 17, 2010 9:52 am
Post
by mickyjune26 » Fri May 21, 2010 5:35 pm
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);
?>
social_experiment
DevNet Master
Posts: 2793 Joined: Sun Feb 15, 2009 11:08 am
Location: .za
Post
by social_experiment » Sat May 22, 2010 5:39 am
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>'; ?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Chalks
Forum Contributor
Posts: 447 Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana
Post
by Chalks » Sat May 22, 2010 11:03 pm
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);
mickyjune26
Forum Commoner
Posts: 30 Joined: Mon May 17, 2010 9:52 am
Post
by mickyjune26 » Mon May 24, 2010 7:55 am
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]
Chalks
Forum Contributor
Posts: 447 Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana
Post
by Chalks » Mon May 24, 2010 9:08 am
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
}
mickyjune26
Forum Commoner
Posts: 30 Joined: Mon May 17, 2010 9:52 am
Post
by mickyjune26 » Mon May 24, 2010 9:20 am
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.
Chalks
Forum Contributor
Posts: 447 Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana
Post
by Chalks » Mon May 24, 2010 11:06 am
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.
mickyjune26
Forum Commoner
Posts: 30 Joined: Mon May 17, 2010 9:52 am
Post
by mickyjune26 » Mon May 24, 2010 11:21 am
thanks for the tip. It's always good to be directed to a good tutorial.