Jump to content

Recommended Posts

Ok the code is short very! so i dont need to really explain what i am trying to do,

 

test.php

 

<?php

$textfile = 'datafiles.rtf';

$handle = fopen("$textfile", "r");

$contents = fread($handle, filesize($textfile));

fclose($handle);

echo nl2br($contents);
echo "<BR>";

$det = preg_split("/[\s,]+/", "$contents");



print_r($det);

?>

 

 

write.php

 

<?php

  foreach(glob("*.php") as $filename) {

    $contents .= $filename . '-' . filesize($filename) . "\r" ;

  }

  file_put_contents("datafiles.rtf", $contents);

?>

 

datafiles.rtf

 

connect.php-7722
db.php-1482
test.php-281
write.php-180

 

Screen Print

 

connect.php-7722

db.php-1482

test.php-281

write.php-180

 

Array ( [0] => connect.php-7722 [1] => db.php-1482 [2] => test.php-281 [3] => write.php-180 [4] => )

 

 

 

i need to turn $det into and array like so array($FILENAME => $SIZE) with all the details on the files in one array basically i need to remove the " - " from the .rtf file (implode, explode, split, str_replace) anything i am just really stuck here :(

 

James.

Link to comment
https://forums.phpfreaks.com/topic/166432-i-need-array-help/
Share on other sites

test.php

 

<?php

$textfile = 'datafiles.rtf';

$handle = fopen("$textfile", "r");

$contents = fread($handle, filesize($textfile));

fclose($handle);

echo nl2br($contents);
echo "<BR>";

$det = preg_split("/[\s,]+/", "$contents");



print_r($det);

?>

That can be written as

$contents = file('datafiles.rtf');
$contents = array_map('trim', $contents);

foreach($contents as $line)
{
     list($filename, $size) = explode('-', $line);

     $files[$filename] = $size;
}

echo '<pre>'.print_r($files, true).'</pre>';

Link to comment
https://forums.phpfreaks.com/topic/166432-i-need-array-help/#findComment-877673
Share on other sites

test.php

 

<?php

$textfile = 'datafiles.rtf';

$handle = fopen("$textfile", "r");

$contents = fread($handle, filesize($textfile));

fclose($handle);

echo nl2br($contents);
echo "<BR>";

$det = preg_split("/[\s,]+/", "$contents");



print_r($det);

?>

That can be written as

$contents = file('datafiles.rtf');
$contents = array_map('trim', $contents);

foreach($contents as $line)
{
     list($filename, $size) = explode('-', $line);

     $files[$filename] = $size;
}

echo '<pre>'.print_r($files, true).'</pre>';

 

hiya mate,

 

this seems to be very effective :),

 

but the only problem is, is that it is showing of the details of one file (one line)

 

this is the screen print

 

 

Array

(

    [connect.php] => 7722

db.php

)

Link to comment
https://forums.phpfreaks.com/topic/166432-i-need-array-help/#findComment-877680
Share on other sites

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.