Jump to content

Recommended Posts

Hi,

 

I currently produce a directory listing which simply shows the name of the file as a link. The code for this is below:

<?php

$link  = "dol_2011\_feb";

    if (file_exists($link)) 
    
    echo "<h2>February</h2>";
    
    {
        if ($handle = opendir($link))

			{ 
    				
    				while (false !== ($file = readdir($handle)))
    				
    					{  
       					
       						if ($file != "." && $file != "..")
       						
       							{  
           							if(!preg_match("/\.pdf$/", $file)) continue;
           							
           								{
           									
           									echo "<a href=\"".$link."/".$file."\">".substr_replace($file ,"",-4)."</a><br>";}
        								
        						}  
        						
        				}
        				
				echo "<br>"; 
    					
    				closedir($handle); }



    }
    
?>

 

This works perfectly, although the files in the folder are all named "DOLddmmyy.pdf" (obviously ddmmyy is the current date). The current script removes the ".pdf", but I would like to be able to format the text like dd/mm/yy. Is it possible to apply all these changes on the same string? Renaming the files isn't possible I'm afraid, so I'm stuck with the current file names.

 

Many thanks

Link to comment
https://forums.phpfreaks.com/topic/251376-substr_replace-query/
Share on other sites

easiest way I could come up with

 

<?php
$string = "DOLddmmyy.pdf";
$string_day = substr($string, 3, 2);
$string_month = substr($string, 5, 2);
$string_year = substr($string, 7, 2);

$combine_strings = "$string_day/$string_month/$string_year";

echo $combine_strings;
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.