Jump to content

substr_replace query


Icebergness

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;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.