Raz0r Posted November 16, 2009 Share Posted November 16, 2009 Ok guys...I need some help... I do not have idea how to do this: I need PHP code that will open a text document and read it, and store it in the array, in this way. $a=array("1" = "example", "2" = "example2"......., so each line has its number in array...) I hope that this is possible. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/181720-php-array-from-file/ Share on other sites More sharing options...
joel24 Posted November 16, 2009 Share Posted November 16, 2009 $file_handle = fopen("yourtextdoc.txt", "rb"); $textArray = array(); while (!feof($file_handle) ) { $line_of_text = fgets($file_handle); $textArray[] = $line_of_text; } fclose($file_handle); //now your lines of text are stored in $textArray Quote Link to comment https://forums.phpfreaks.com/topic/181720-php-array-from-file/#findComment-958421 Share on other sites More sharing options...
Raz0r Posted November 16, 2009 Author Share Posted November 16, 2009 Hmmm.... It is not working.When I echo it, it just displays "Array" on screen. Quote Link to comment https://forums.phpfreaks.com/topic/181720-php-array-from-file/#findComment-958426 Share on other sites More sharing options...
cags Posted November 16, 2009 Share Posted November 16, 2009 You cannot just echo an array. If you wish to view the contents you should use something like print_r. Quote Link to comment https://forums.phpfreaks.com/topic/181720-php-array-from-file/#findComment-958430 Share on other sites More sharing options...
salathe Posted November 16, 2009 Share Posted November 16, 2009 The file function will read a file into an array of the lines. Is that what you want? Quote Link to comment https://forums.phpfreaks.com/topic/181720-php-array-from-file/#findComment-958435 Share on other sites More sharing options...
Raz0r Posted November 16, 2009 Author Share Posted November 16, 2009 Thank you, joel24 and cags. You solved my problem. I haven't expected so fast answer. Thank you one more time One more thing... Is it possible to do the same thing, but from textbox? Quote Link to comment https://forums.phpfreaks.com/topic/181720-php-array-from-file/#findComment-958446 Share on other sites More sharing options...
cags Posted November 16, 2009 Share Posted November 16, 2009 Yes, but that would depend on how you wished to split the data into an array. You could simply explode the data on \r\n, which would give a similar result to reading it from a file. Quote Link to comment https://forums.phpfreaks.com/topic/181720-php-array-from-file/#findComment-958455 Share on other sites More sharing options...
Raz0r Posted November 16, 2009 Author Share Posted November 16, 2009 One more question... How do i send mail to addresses in array? Let's say i have this: <?php $to = I need addresses from array to be here. $subject = 'the subject'; $message = 'hello'; $headers = 'From: webmaster@example.com' . "\r\n" . 'Reply-To: webmaster@example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers); ?> Quote Link to comment https://forums.phpfreaks.com/topic/181720-php-array-from-file/#findComment-958467 Share on other sites More sharing options...
cags Posted November 16, 2009 Share Posted November 16, 2009 Personally I wouldn't put them where you have marked. $emails = array("bob@thebuilder.com", "jim@thebuilder.com", "fred@thebuilder.com"); $to = $emails[0]; unset($emails[0]); $bcc = implode("; ", $emails); $headers = 'From: webmaster@example.com' . "\r\n"; $headers .= 'Reply-To: webmaster@example.com' . "\r\n"; $headers .= 'BCC: ' . $bcc; $headers .= 'X-Mailer: PHP/' . phpversion(); Quote Link to comment https://forums.phpfreaks.com/topic/181720-php-array-from-file/#findComment-958474 Share on other sites More sharing options...
Raz0r Posted November 16, 2009 Author Share Posted November 16, 2009 WOW MAN!!! I got it now!!! Some things are a lot of clearer to me now... Thank you!!!!!!!!!!!! Quote Link to comment https://forums.phpfreaks.com/topic/181720-php-array-from-file/#findComment-958631 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.