ballhogjoni Posted October 9, 2008 Share Posted October 9, 2008 I have a txt file that has a lot of ip addresses and they are aligned like this: 58.2.0.0 58.2.255.255 58.68.0.0 58.68.127.255 58.146.96.0 58.146.127.255 59.88.0.0 59.99.255.255 I want to replace the space with a new line. I tried this code: $lines = file_get_contents('asdfsdf.txt'); $myFile = "testFile.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); fwrite($fh, str_replace(' ','\n',$lines)); fclose($fh); which replaces the space with the \n but it doesn't add a new line to the txt file. any ideas. My end goal is to enter this txt file into my db. Link to comment https://forums.phpfreaks.com/topic/127773-replace-space-with-new-line/ Share on other sites More sharing options...
DarkWater Posted October 9, 2008 Share Posted October 9, 2008 Use your same code, except put \n in " " instead of ' '. \n means nothing in single quotes. Link to comment https://forums.phpfreaks.com/topic/127773-replace-space-with-new-line/#findComment-661380 Share on other sites More sharing options...
ballhogjoni Posted October 9, 2008 Author Share Posted October 9, 2008 ok that worked great thanks. Now I tried to insert each line of the file into my db creating a new row for each line. It didn't work and I don't know where I went wrong. Any ideas? <?php $lines = file_get_contents('testFile.txt'); $sa = array($lines); foreach($sa as $num => $line){ mysql_query('INSERT INTO ip_tracking (ip) values ("' . mysql_real_escape_string($line) . '")'); } ?> Link to comment https://forums.phpfreaks.com/topic/127773-replace-space-with-new-line/#findComment-661411 Share on other sites More sharing options...
kenrbnsn Posted October 9, 2008 Share Posted October 9, 2008 That will not get you an array of lines, you want to use the file() function instead: <?php $lines = file('testFile.txt'); foreach ($lines as $line) { $q = "INSERT INTO ip_tracking (ip) values ('" . mysql_real_escape_string(trim($line)) . "')"; $rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error()); } ?> Ken Link to comment https://forums.phpfreaks.com/topic/127773-replace-space-with-new-line/#findComment-661415 Share on other sites More sharing options...
ballhogjoni Posted October 9, 2008 Author Share Posted October 9, 2008 that worked great thanks, I have a problem with it only inserting 1000 rows. the structure of the table is id bigint (50) auto_increment ip varchar (50) Link to comment https://forums.phpfreaks.com/topic/127773-replace-space-with-new-line/#findComment-661423 Share on other sites More sharing options...
ballhogjoni Posted October 9, 2008 Author Share Posted October 9, 2008 nevermind i am officially stupid, the 1000 rows is what I limited my view to see. I forgot the nice little next button. Link to comment https://forums.phpfreaks.com/topic/127773-replace-space-with-new-line/#findComment-661424 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.