Page 1 of 1

Remove file extensions from array ?

Posted: Sun Oct 12, 2008 4:37 pm
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); 
 

Re: Remove file extensions from array ?

Posted: Sun Oct 12, 2008 5:14 pm
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

Re: Remove file extensions from array ?

Posted: Sun Oct 12, 2008 5:19 pm
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);

Re: Remove file extensions from array ?

Posted: Sun Oct 12, 2008 5:39 pm
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?

Re: Remove file extensions from array ?

Posted: Sun Oct 12, 2008 5:53 pm
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.

Re: Remove file extensions from array ?

Posted: Sun Oct 12, 2008 6:34 pm
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