Jump to content

[SOLVED] Array removing line breaks


inspireddesign

Recommended Posts

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;
   }


 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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:  ;D

 


<?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;
    }

?>

 

   

Link to comment
Share on other sites

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" );

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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";
?>

Link to comment
Share on other sites

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";

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.