inspireddesign Posted April 15, 2009 Share Posted April 15, 2009 Hello, The below Array for some reason is removing anything with \n in it. Can someone tell me why this would be happening and where in the code? Is the str_replace function removing the slashes? Thanks for the help in advance! function modifier($vars, $rftfile) { $xchange = array (); $document = file_get_contents($rftfile); if(!$document) { return false; } foreach($vars as $key=>$value) { $search = "%%".strtoupper($key)."%%"; foreach($xchange as $orig => $replace) { $value = str_replace($orig, $replace, $value); } $document = str_replace($search, $value, $document); } return $document; } Quote Link to comment https://forums.phpfreaks.com/topic/154270-solved-array-removing-line-breaks/ Share on other sites More sharing options...
Axeia Posted April 15, 2009 Share Posted April 15, 2009 Are you sure they are actually removed? If your echo'ing the return value, and it's not between <pre></pre> tags or another tags styled with white-space: nowrap; then they're still there.. they simply just don't show up. There is a nl2br function to get around that though. Quote Link to comment https://forums.phpfreaks.com/topic/154270-solved-array-removing-line-breaks/#findComment-811034 Share on other sites More sharing options...
inspireddesign Posted April 15, 2009 Author Share Posted April 15, 2009 Axeia, It sure is removing the \n from the string. I can't use the nl2br function because it returns a <br> and I can't use that output for what I'm doing. For whatever reason the code is stripping the \n from the string. I'm outputting the string to a txt document so I know that it's not there. Someone taught me something new: <?php function modifier($vars, $rftfile) { $xchange = array (); $document = file_get_contents($rftfile); if(!$document) { return false; } foreach($vars as $key=>$value) { $search = "%%".strtoupper($key)."%%"; foreach($xchange as $orig => $replace) { $value = str_replace($orig, $replace, $value); } $document = str_replace($search, $value, $document); } return $document; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/154270-solved-array-removing-line-breaks/#findComment-811045 Share on other sites More sharing options...
Carth Posted April 15, 2009 Share Posted April 15, 2009 What sort of thing is in $vars, and what is in $xchange? In the code above, the latter is just an empty array so the inner foreach loop will never happen. Quote Link to comment https://forums.phpfreaks.com/topic/154270-solved-array-removing-line-breaks/#findComment-811052 Share on other sites More sharing options...
inspireddesign Posted April 15, 2009 Author Share Posted April 15, 2009 This is what's in the $vars array. I tried to use the xchange array to change the \n from /nl to \n to try and "trick" the array but it didn't work. <?php $xchange = array ('/nl' => '\n'); ?> <?php $vars = array( 'info1' => "Line One \n Line Two", 'info2' => "Line Three \n Line Four" ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/154270-solved-array-removing-line-breaks/#findComment-811058 Share on other sites More sharing options...
Carth Posted April 15, 2009 Share Posted April 15, 2009 Are you absolutely sure the new lines are disappearing? Because I really can't see why. If you output text to the browser you need to send a header at the top of your script: header("Content-type: text/plain"); Otherwise the browser won't display the new lines because it thinks it's HTML still. Oh, another thing to consider. Depending on how you are viewing this text, you might want to use a different end of line character. Windows expects "\r\n", while Unix just expects "\n". If you are transferring the file in binary mode FTP or something weird, then your new lines won't display. Quote Link to comment https://forums.phpfreaks.com/topic/154270-solved-array-removing-line-breaks/#findComment-811064 Share on other sites More sharing options...
inspireddesign Posted April 16, 2009 Author Share Posted April 16, 2009 After reviewing... the array is NOT removing the \n from the string. It's somewhere else in my app. Sorry for the mix up and thanks for the help. This works fine. <?php function modifier($vars, $rftfile) { $document = file_get_contents($rftfile); if(!$document) { return false; } foreach($vars as $key=>$value) { $search = "%%".strtoupper($key)."%%"; $document = str_replace($search, $value, $document); } return $document; } $orgFile = "orgFile.rtf"; $newFile = "newFile.rtf"; $vars = array( 'info1' => "Line One\n Line Two", 'info2' => "Line Three\n Line Four" ); $fData = modifier($vars, $orgFile); $Handle = fopen($newFile, 'w'); fwrite($Handle, $fData); fclose($Handle); print "Data Written"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/154270-solved-array-removing-line-breaks/#findComment-811084 Share on other sites More sharing options...
inspireddesign Posted April 16, 2009 Author Share Posted April 16, 2009 Okay... I've figured out my problem but have a new one. I'm writing to a Rich Text Format (RTF) document and it doesn't like the \n or <br /> or <br> or chr(13) or anything else that could return the string to the next line. How I can pass the sting to a RTF document with line breaks. here is a sample of what I'm trying to accomplish. Thanks again for all the help. To test you'll want to create a file called orgFile.rtf with %%INFO1%% and %%INFO2%% in the body. <?php function modifier($vars, $rftfile) { $document = file_get_contents($rftfile); if(!$document) { return false; } foreach($vars as $key=>$value) { $search = "%%".strtoupper($key)."%%"; $document = str_replace($search, $value, $document); } return $document; } $orgFile = "orgFile.rtf"; $newFile = "newFile.rtf"; $vars = array( 'info1' => "Line One\n Line Two", 'info2' => "Line Three\n Line Four" ); $fData = modifier($vars, $orgFile); $Handle = fopen($newFile, 'w'); fwrite($Handle, $fData); fclose($Handle); print "Data Written"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/154270-solved-array-removing-line-breaks/#findComment-811110 Share on other sites More sharing options...
premiso Posted April 16, 2009 Share Posted April 16, 2009 Not sure on rtf and how that really works, but maybe it needs \r\n and not just \n. I would try that and see where that gets you. Quote Link to comment https://forums.phpfreaks.com/topic/154270-solved-array-removing-line-breaks/#findComment-811135 Share on other sites More sharing options...
kenrbnsn Posted April 16, 2009 Share Posted April 16, 2009 You can download the latest RTF specs from Microsoft. That should tell you what you need to know. Ken Quote Link to comment https://forums.phpfreaks.com/topic/154270-solved-array-removing-line-breaks/#findComment-811162 Share on other sites More sharing options...
inspireddesign Posted April 16, 2009 Author Share Posted April 16, 2009 THANKS kenrbnsn!!! Exactly what I needed! The syntax is \line NOT \n Quote Link to comment https://forums.phpfreaks.com/topic/154270-solved-array-removing-line-breaks/#findComment-811422 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.