10 Replies - 1266 Views - Last Post: 27 August 2002 - 02:16 PM Rate Topic: -----

#1 fyrestorm  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 10
  • View blog
  • Posts: 3,113
  • Joined: 04-April 02

Taking Apart Strings

Posted 26 August 2002 - 10:10 AM

ok, this is probably a really stupid question but it's important to me:

using php, how do i read in a file name, say like a dated file: 08262002.htm and i know that i want to read it into a variable, with the first two digits as month, the second two as date, and then the last four as year...

is there a way to do that?
Is This A Good Question/Topic? 0
  • +

Replies To: Taking Apart Strings

#2 Cookie Mobster  Icon User is offline

  • nooneenooneenooonee
  • member icon

Reputation: 7
  • View blog
  • Posts: 4,730
  • Joined: 12-October 01

Re: Taking Apart Strings

Posted 26 August 2002 - 11:00 AM

http://www.php.net/m...tion.substr.php :P
Was This Post Helpful? 0
  • +
  • -

#3 Spider  Icon User is offline

  • Arachnid

Reputation: 2
  • View blog
  • Posts: 769
  • Joined: 10-July 02

Re: Taking Apart Strings

Posted 26 August 2002 - 11:00 AM

it would be simple to do if the date was formatted 00-00-0000 (or with any other seperator)

I'm not sure how to do it without.

I was just wondering though, how are you planning on grabbing the url?

and is it stored as a value?
Was This Post Helpful? 0
  • +
  • -

#4 fyrestorm  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 10
  • View blog
  • Posts: 3,113
  • Joined: 04-April 02

Re: Taking Apart Strings

Posted 26 August 2002 - 12:04 PM

i could separate it, but the idea wasn't to get it from an url it was to make the url from the file name, so i'd have a thing that looked through a directory and the code would create the url, if that makes any sense at all...
Was This Post Helpful? 0
  • +
  • -

#5 fyrestorm  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 10
  • View blog
  • Posts: 3,113
  • Joined: 04-April 02

Re: Taking Apart Strings

Posted 26 August 2002 - 12:09 PM

smckone, on Aug 26 2002, 12:00 PM, said:


thanks sam, that will work nicely, now i just have to figure out how to read what's in a directory...
Was This Post Helpful? 0
  • +
  • -

#6 Cookie Mobster  Icon User is offline

  • nooneenooneenooonee
  • member icon

Reputation: 7
  • View blog
  • Posts: 4,730
  • Joined: 12-October 01

Re: Taking Apart Strings

Posted 26 August 2002 - 12:50 PM

$dir = opendir("files");
while(($im = readdir($dir))) {
    echo $im;
}  
closedir($dir);


Was This Post Helpful? 0
  • +
  • -

#7 iordy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 05-March 02

Re: Taking Apart Strings

Posted 26 August 2002 - 08:12 PM

Just for the record, regular expressions are much better at this sort of thing. For instance you can grab and seperate the whole file name in one go.

<?
$filename = '08262002.htm';
preg_match('/(\d{2})(\d{2})(\d{4}).*/', $filename, $match);

# just to show where everything is in match;
$filedate = array (
    'month' => $match[1],
    'day'   => $match[2],
    'year'  => $match[3]
);
print_r($filedate);
?>


Was This Post Helpful? 0
  • +
  • -

#8 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1865
  • View blog
  • Posts: 20,278
  • Joined: 17-March 01

Re: Taking Apart Strings

Posted 26 August 2002 - 10:37 PM

$handle=opendir('/hsphere/local/home/dreaminc/home.dreamincode.net/newsletters'); 

 	 while (false!==($file = readdir($handle))) { 

    if ($file != "." && $file != ".." && $file != "newsletters.php") {

   	 $filename = str_replace("_"," ",$file);

                    $month = substr("$filename", 0, 2); 

   	 $day = substr("$filename", 2, 2);

   	 $year = substr("$filename", 4, 2);

   	 $year = "20".$year;

   	 $newsletter_date = mktime (0,0,0,$month,$day,$year);

   	 $newsletter_date = date("F d, Y", $newsletter_date);

   	 $filename = str_replace(".php","",$filename);

   	 echo "<li><b><a href='/newsletters/$file' target=_blank>$newsletter_date</a></b></li>"; 

    } 



We use that code in our newsletter page to get all the newsletter files which are formatted mmddyy.php/.txt

http://home.dreaminc...hp?p=newsletter is the end result.
Was This Post Helpful? 0
  • +
  • -

#9 fyrestorm  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 10
  • View blog
  • Posts: 3,113
  • Joined: 04-April 02

Re: Taking Apart Strings

Posted 27 August 2002 - 07:44 AM

skyhawk133, on Aug 26 2002, 11:37 PM, said:

$handle=opendir('/hsphere/local/home/dreaminc/home.dreamincode.net/newsletters'); 

  	while (false!==($file = readdir($handle))) { 

    if ($file != "." && $file != ".." && $file != "newsletters.php") {

    	$filename = str_replace("_"," ",$file);

                    $month = substr("$filename", 0, 2); 

    	$day = substr("$filename", 2, 2);

    	$year = substr("$filename", 4, 2);

    	$year = "20".$year;

    	$newsletter_date = mktime (0,0,0,$month,$day,$year);

    	$newsletter_date = date("F d, Y", $newsletter_date);

    	$filename = str_replace(".php","",$filename);

    	echo "<li><b><a href='/newsletters/$file' target=_blank>$newsletter_date</a></b></li>"; 

    } 



We use that code in our newsletter page to get all the newsletter files which are formatted mmddyy.php/.txt

http://home.dreaminc...hp?p=newsletter is the end result.

wow, thanks chris, that's greatly appreciated :)
Was This Post Helpful? 0
  • +
  • -

#10 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1865
  • View blog
  • Posts: 20,278
  • Joined: 17-March 01

Re: Taking Apart Strings

Posted 27 August 2002 - 12:59 PM

No problem, that's what dream.in.code is for ;)
Was This Post Helpful? 0
  • +
  • -

#11 fyrestorm  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 10
  • View blog
  • Posts: 3,113
  • Joined: 04-April 02

Re: Taking Apart Strings

Posted 27 August 2002 - 02:16 PM

of course B)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1