markjoe Posted August 16, 2007 Share Posted August 16, 2007 Notice that Daniel0's function returns the value rather than echoing it. Which I think is a much better idea. But that means you need to "echo changeName($name)" rather than just calling it. Quote Link to comment Share on other sites More sharing options...
wrathican Posted August 16, 2007 Author Share Posted August 16, 2007 right i see. now i have a problem when using it though. i *think* im using it correctly heres the difference: before the function: after using the function: any idea why its doing this? heres the code im using when using the function: <?php function changeName($file) { $path_parts = pathinfo($file); return ucfirst(strtolower(str_replace(array('-', '_', ' '), ' ', $path_parts['filename']))); } $dirs = array(); $imgs = array(); $others = array(); $img_extensions = array(".gif", ".png", ".jpg", ".jpeg", ".jpe", ".bmp"); $files = scandir("."); foreach($files as $file) { if($file != "." && $file != "..") { $ext = strtolower(strrchr($file,".")); if(is_dir($file)) $dirs[] = $file; elseif(in_array($ext, $img_extensions)) $imgs[] = $file; else $others[] = $file; } } if(count($dirs) > 0) { echo "<b>Directories</b><ul>"; foreach ($dirs as $file) echo "<li><a href='".$file."'>"; echo changeName($file); echo "</a></li>"; echo "</ul>"; } if(count($imgs) > 0) { echo "<b>Images</b><ul>"; foreach ($imgs as $file) echo "<li><a href='".$file."'><img src='".$file."' width='25' border='0'> "; echo changeName($file); echo "</a></li>"; echo "</ul>"; } if(count($others) > 0) { echo "<b>Other Files</b><ul>"; foreach ($others as $file) echo "<li><a href='".$file."'>"; echo changeName($file); echo "</a></li>"; echo "</ul>"; } ?> Quote Link to comment Share on other sites More sharing options...
Orio Posted August 16, 2007 Share Posted August 16, 2007 EDIT- Read next post, not this!! I guess you are using PHP < 5.2.0 Use this function instead: <?php function changeName($file) { $file = substr($file, 0, -(strlen(strrchr($file, ".")))); return ucfirst(strtolower(str_replace(array('-', '_', ' '), ' ', $file))); } ?> Orio. Quote Link to comment Share on other sites More sharing options...
Orio Posted August 16, 2007 Share Posted August 16, 2007 lol I found the problem... Curly braces. When I orginally made the script each foreach had one line so I omitted the curly braces. But after you add more lines you have put them back. Do it this way: <?php function changeName($file) { $cut = -(strlen(strrchr($file, "."))); if($cut != 0) $file = substr($file, 0, $cut); return ucfirst(strtolower(str_replace(array('-', '_', ' '), ' ', $file))); } $dirs = array(); $imgs = array(); $others = array(); $img_extensions = array(".gif", ".png", ".jpg", ".jpeg", ".jpe", ".bmp"); $files = scandir("."); foreach($files as $file) { if($file != "." && $file != "..") { $ext = strtolower(strrchr($file,".")); if(is_dir($file)) $dirs[] = $file; elseif(in_array($ext, $img_extensions)) $imgs[] = $file; else $others[] = $file; } } if(count($dirs) > 0) { echo "<b>Directories</b><ul>"; foreach ($dirs as $file) { echo "<li><a href='".$file."'>"; echo changeName($file); echo "</a></li>"; } echo "</ul>"; } if(count($imgs) > 0) { echo "<b>Images</b><ul>"; foreach ($imgs as $file) { echo "<li><a href='".$file."'><img src='".$file."' width='25' border='0'> "; echo changeName($file); echo "</a></li>"; } echo "</ul>"; } if(count($others) > 0) { echo "<b>Other Files</b><ul>"; foreach ($others as $file) { echo "<li><a href='".$file."'>"; echo changeName($file); echo "</a></li>"; } echo "</ul>"; } ?> I also made it work with php < 5.2.0 Orio. Quote Link to comment Share on other sites More sharing options...
wrathican Posted August 16, 2007 Author Share Posted August 16, 2007 ohhh yeah! i did notice them last night when you first posted the code but being a n00b didnt think anything of it ahh now it works! thanks all for all your great help! thnks ever so much! Quote Link to comment Share on other sites More sharing options...
Orio Posted August 16, 2007 Share Posted August 16, 2007 No problem Enjoy. Orio. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.