Jump to content

PHP - Splitting a File?


sam06

Recommended Posts

I'm sure many of you are familiar with Project Euler - now I'm on Problem 67.

 

I'm hoping you can help me out - I'm trying to split a txt file into parts.

It's located at http://projecteuler.net/project/triangle.txt

 

Now, I want to split it into numbers, then perform an action, and loops untill finishing.

 

I'm not too sure how I do this - So I capture the first 2 characters, move forward 1 (does this apply when going onto a new line?), then capture the next 2 characters etc.

 

Can anyone help me?

 

Much appreciated,

Sam

Link to comment
https://forums.phpfreaks.com/topic/171552-php-splitting-a-file/
Share on other sites

if you want to get each number, that is each number between spaces, load the file into a array, loop through each line, explode each line by " ", then loop through each member of the new array - that would give you your numbers, and perform whatever function you want on it

Link to comment
https://forums.phpfreaks.com/topic/171552-php-splitting-a-file/#findComment-904698
Share on other sites

So far I've got:

 

<?php
$file=fopen("triangle.txt","r") or exit("Unable to open file!");
while (!feof($file)) {

$character = fgetc($file)  ;
if($character != " "){
// Perform Action
echo $character;

//
} else { 
echo '<br>';

}
  
  }
fclose($file);
?> 

 

But that doesn't count the "new line" as a character :(

Link to comment
https://forums.phpfreaks.com/topic/171552-php-splitting-a-file/#findComment-904699
Share on other sites

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.