Remove file extensions from array ?

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

Post Reply
ianhman
Forum Newbie
Posts: 6
Joined: Sun Oct 12, 2008 4:34 pm

Remove file extensions from array ?

Post by ianhman »

Hi everyone,

Just signed up this forum, great to see so many active posts with replies!

Anyway, straight into the coding issue and I would be very gratetful for your help....

I am displaying a list of PDF files from a folder using an array.

Currently the output looks something like this:

<a href='test/A.pdf'>A.pdf</a>
<a href='test/B.pdf'>B.pdf</a>



But the output should be like this (without displaying file extension on the web page) :

<a href='test/A.pdf'>A</a>
<a href='test/B.pdf'>B</a>



I have tried a number of ways to prevent the file extension from displaying on the web page, but no matter what I try the file extension keeps appearing :confused:

I would really appreciate some help on this :)


Here is my full PHP code:

Code: Select all

 
                    //get folder name from query string
                    $str_subcat = $_GET['subcat'];
                    
                    $Folder = "pdfs/$str_subcat/";
                    
                    $narray=array();
                    $i=0;
              
                    // Open the folder                 
                    $DirHandle = @opendir($Folder) or die($Folder." could not be opened.");           
              
              
                    while($file = readdir($DirHandle))
                    {
                        if(is_dir($file))
                        {
                            continue;
                        }
                       //remove . and .. characters displayed by server.
                       //only display PDF files in the directory.
                        else if($file != '.' && $file != '..' && strpos($file, '.pdf'))
                        {
                            //echo "<a href='$path/$file'>$file</a><br/>";
                            
                            $narray[$i]=$file;                            
                            $i++;                                                        
                        }
                        
                    }
                    sort($narray);
 
 
 
                    for($i=0; $i<sizeof($narray); $i++)
                    {
                    
                        echo "<tr>";
                        echo "<td width=100% class=bodycopy>";
                        echo "<a href='".$Folder.$narray[$i].$file."' target=blank>".$narray[$i].$file."</a>";
                        echo "</td>";
                    }
 
 
                    // Close the handle to the directory
                    closedir($DirHandle); 
 
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Remove file extensions from array ?

Post by aceconcepts »

use explode to just get the file name and not the extension:

Code: Select all

 
$exp=explode(".", $file);
$file_name_only=$exp[0];
 
http://uk.php.net/explode
User avatar
omika
Forum Newbie
Posts: 19
Joined: Sun Oct 12, 2008 2:00 pm
Location: New Zealand

Re: Remove file extensions from array ?

Post by omika »

You could also wipe the extension with str_replace() if your only using the .pdf extension.

Code: Select all

$narray[$i]=str_replace(".pdf", "", $file);
ianhman
Forum Newbie
Posts: 6
Joined: Sun Oct 12, 2008 4:34 pm

Re: Remove file extensions from array ?

Post by ianhman »

omika wrote:You could also wipe the extension with str_replace() if your only using the .pdf extension.

Code: Select all

$narray[$i]=str_replace(".pdf", "", $file);
Hi omika,

Your suggestion solves half the problem, but it takes out all the PDF file extensions from the whole output. It should only take out the PDF file extension from link text, not the urls/anchor tags.

Current output on web page:
<a href='test/A'>A</a>
<a href='test/B'>B</a>


Desired output on the web page should look like this:

<a href='test/A.pdf'>A</a>
<a href='test/B.pdf'>B</a>


Any ideas how I can just have the PDF extension appearing in the urls/anchor tags and not the display text?
User avatar
omika
Forum Newbie
Posts: 19
Joined: Sun Oct 12, 2008 2:00 pm
Location: New Zealand

Re: Remove file extensions from array ?

Post by omika »

Whoops :oops: ,

Code: Select all

echo "<a href='".$Folder.$narray[$i].$file."' target=blank>".$narray[$i].$file."</a>";
try

Code: Select all

echo "<a href='".$Folder.$narray[$i].$file."' target=blank>".str_replace(".pdf", "", $narray[$i].$file)."</a>";
hopefully that gets the result you need.
ianhman
Forum Newbie
Posts: 6
Joined: Sun Oct 12, 2008 4:34 pm

Re: Remove file extensions from array ?

Post by ianhman »

omika wrote:Whoops :oops: ,

Code: Select all

echo "<a href='".$Folder.$narray[$i].$file."' target=blank>".$narray[$i].$file."</a>";
try

Code: Select all

echo "<a href='".$Folder.$narray[$i].$file."' target=blank>".str_replace(".pdf", "", $narray[$i].$file)."</a>";
hopefully that gets the result you need.
Thanks so much omika! That worked a treat, problem now solved :D
Post Reply