Explode inserts spaces between each character in string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • need2know2
    New Member
    • Mar 2008
    • 3

    Explode inserts spaces between each character in string

    I'm filling an array with explode(";" $linetoread) and then using the array to write to a file. When I open the file the array elements have spaces between each character. I've tried split as well with the same results. If viewed in a browser, all the spaces are missing even though they exist in the source.
  • bonski
    New Member
    • Jun 2007
    • 53

    #2
    Originally posted by need2know2
    I'm filling an array with explode(";" $linetoread) and then using the array to write to a file. When I open the file the array elements have spaces between each character. I've tried split as well with the same results. If viewed in a browser, all the spaces are missing even though they exist in the source.
    hi there,

    can you share us the script so that we can figure out what's wrong.

    bonski

    Comment

    • arggg
      New Member
      • Mar 2008
      • 91

      #3
      try using the trim($variable) ; function to trim out all the spaces.

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Trim() does not remove embedded blanks, just the white space at start and end of a vriable. To remove embedded blanks combine trim() with a reg exp like[php]$string = trim (preg_replace ('`[\s]+`', ' ', $string));[/php]Ronald

        Comment

        • need2know2
          New Member
          • Mar 2008
          • 3

          #5
          Thanks for the replies.
          Here's the code
          Code:
          // File to copy from
          $fSourcePath = "HCFD-SM.xml";
          $fSourceHandle = fopen($fSourcePath, 'r');
          
          //File to copy to
          $fTargetPath = "test.txt";
          $fTargetHandle = fopen($fTargetPath, 'w');
          
          //MP3Tag file
          $fMP3Source = "mp3tag.csv";
          $fMP3Handle = fopen($fMP3Source, 'r');
          
          
          $thisLine = fgets($fMP3Handle);
          $thisLine = fgets($fMP3Handle);
          $arrMP3 = explode(";", $thisLine);
          
          
          for($i=0;$i<count($arrMP3);$i++)
          {
          	echo $arrMP3[$i] . "<br>";
          }
          Viewed in the browser it looks fine. But then I write to the file:

          Code:
          $currentLine = 0;
          $lineCount = count($arrMP3);
          while($currentLine < $lineCount)
          {
          	fwrite($fTargetHandle, $arrFile[$currentLine]);
          	if(trim($arrFile[$currentLine]) == "</itunes:owner>")
          	{
          		fwrite($fTargetHandle, "       <item>\r\n");
          		fwrite($fTargetHandle, "           <title>" . $title . "</title>\r\n");
          		fwrite($fTargetHandle, "           <link>" . $path . $filename . "</link>\r\n");
          
          // etc
          // etc
          		fwrite($fTargetHandle, "       </item\r\n");
          	}	
          	$currentLine++;
          }
          I have spaces between every character

          Comment

          • satas
            New Member
            • Nov 2007
            • 82

            #6
            It seems that variables $title, $path and $filename have not been initialized.

            Comment

            • need2know2
              New Member
              • Mar 2008
              • 3

              #7
              Satas you are correct. Those files are not initialized because I pasted in old code in which I was using a list rather than an array

              All the fwrites are actually using the array elements created with explode(...)

              Unfortunately, both ways produce the spaces in the strings

              ronverdonk - your solution would work if I didn't have spaces in some of the strings that I want to keep...

              Thanks for the replies

              Comment

              • satas
                New Member
                • Nov 2007
                • 82

                #8
                Originally posted by need2know2
                Those files are not initialized because I pasted in old code in which I was using a list rather than an array
                Could you provide us working, not "old" code?

                Comment

                • hsriat
                  Recognized Expert Top Contributor
                  • Jan 2008
                  • 1653

                  #9
                  Originally posted by need2know2
                  I have spaces between every character
                  If the spaces are between all the characters of the file (including XML tags ie. <item> is displayed as < i t e m >), then you got to do something with the file encoding. That's not explode's mischief.

                  Comment

                  Working...