Jump to content

New Line Help


LackOfLuck

Recommended Posts

hi I'm pretty new to php so I think this would be an easy question for most of you :x. So I have a huge text file.All the words are separated by comma. I want every word on this text to be putted in new line. There are a lot of words so doing it manually wouldn't be worth the effort.

 

First I searched for a string function but couldn't find anything .

maybe this could be solved by an array function.

 

Thanks for your help.

Link to comment
https://forums.phpfreaks.com/topic/166025-new-line-help/
Share on other sites

I'd probably use stream_get_line to get each word individually. Or, if the file is small enough that it can fit in memory all at once, you could use the more simple solution of file_get_contents along with explode.

hi thanks for your reply but can you give me an example on how you would use this?

for example the text is:  text12323, tex2131231,textsat2323,text321131 ... and so on

what should my code look like?

sorry if my questions are stupid but again i'm really new to php and I'm sorry if I annoy you :(

Link to comment
https://forums.phpfreaks.com/topic/166025-new-line-help/#findComment-875609
Share on other sites

The safer way which doesn't load the whole file into memory would look something like this...

 

<?php
$maxLineLength = 1024; // Longest possible 'word'
$oldLineDelimiter = ",";
$newLineDelimiter = "\n"; // <br /> if new lines are to be displayed in the browser

// Open the file
$fileHandle = fopen('/path/to/my/file.txt', 'r'); // Or whatever file name you need
if($fileHandle) {
// Read each line and output them one-by-one
while(!feof($fileHandle)) {
	$word = stream_get_line($fileHandle, $maxLineLength, $oldLineDelimiter);
	echo $word . $newLineDelimiter;
}

// Clean up
fclose($fileHandle);
}
else {
echo 'Unable to open file.';
}
?>

 

I didn't test it, so if there's a bug, you'll have to fix it! Don't just copy the code, try to figure out how it works and why I do things the way I do. Feel free to ask any questions, just know I may be asleep by the time you ask them, but I'd be happy to answer them tomorrow.

Link to comment
https://forums.phpfreaks.com/topic/166025-new-line-help/#findComment-875622
Share on other sites

I'm guessing your file looks a bit like this:

 

text, text, text, text, text, text, text, text, text, text, text, text, etc...

 

So you just want to output each of those comma separated values? If so try the following:

 

<?php

$filename = "<the name of the data file.txt>";
$data = file_get_contents($filename);
$data = explode(",", $data);
if(count($data)){
  echo "<ul>";
  foreach($data as $line){
    echo "<li>{$line}</li>";
  }
  echo "</ul>";
}
?>

 

The above piece of code will output the data as an html list.

n.b. make sure to change the code above to include the name of your data file (1st line).

Link to comment
https://forums.phpfreaks.com/topic/166025-new-line-help/#findComment-875635
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.