Jump to content

help needed on saving word into .txt


t_k_eoh

Recommended Posts

Hi guys,

I am a beginner and I hope you guys can help me...

I need to change a sentence from an input text box into words and store it in a text file (file.txt)

 

Eg. "Can someone pls help me"

 

Can

someone

pls

help

me

 

and then store all these into file.txt file

 

Pls help me...thanks.

Link to comment
https://forums.phpfreaks.com/topic/61793-help-needed-on-saving-word-into-txt/
Share on other sites

try



<?php
$filename = 'test.txt';
$somecontent = "Add this to the file\n";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

    // In our example we're opening $filename in append mode.
    // The file pointer is at the bottom of the file hence
    // that's where $somecontent will go when we fwrite() it.
    if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    // Write $somecontent to our opened file.
    if (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }

    echo "Success, wrote ($somecontent) to file ($filename)";

    fclose($handle);

} else {
    echo "The file $filename is not writable";
}
?> 

Hi,

I would like to know how to break a long sentence into words, eg. "can someone pls help me"

into:

 

can

someone

pls

help

me

 

eg. using some sort of array method to store the words and then save it into a .txt file. Appreciate your help. Thanks

 

 

 

<?
$string = "can someone pls help me";
$str_pcs = explode(' ', $string);

$str = "";
foreach($str_pcs as $word):
   $str .= $word."<br>";
endforeach;

# create filename... * You can also put a dir. before the filename eg. "somedir/filename";
$txtfile = 'test.txt';

if (is_writable($txtfile )):
    if (!$handle = fopen($filename, 'w+')):
         echo "Cannot open file ".$txtfile;
else:
	$opent_content = fopen($filename, 'w+'); 
	$write_content = fwrite($filename, $str);
	if($write_content):
		@chmod($txtfile, 0777);
		echo "Success, wrote ".$str" to file ".$txtfile;
	else:
		echo "Cannot write to file ".$txtfile;
	endif;
endif;
    fclose($handle);
endif;
?>

 

@teng84 try to read the question first!! (natuto ka nanaman sakin hahaha!)

Hi DeadEvil,

Thanks for the help but I found this message error:

 

Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in /home/content/p/e/n/penangcenter/html/word/test1.php on line 21

 

what's the reason? Pls advice...

Thanks!

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.