473,378 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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.
Mar 19 '08 #1
8 2476
bonski
53
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
Mar 19 '08 #2
arggg
91
try using the trim($variable); function to trim out all the spaces.
Mar 19 '08 #3
ronverdonk
4,258 Expert 4TB
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
Mar 19 '08 #4
Thanks for the replies.
Here's the code
Expand|Select|Wrap|Line Numbers
  1. // File to copy from
  2. $fSourcePath = "HCFD-SM.xml";
  3. $fSourceHandle = fopen($fSourcePath, 'r');
  4.  
  5. //File to copy to
  6. $fTargetPath = "test.txt";
  7. $fTargetHandle = fopen($fTargetPath, 'w');
  8.  
  9. //MP3Tag file
  10. $fMP3Source = "mp3tag.csv";
  11. $fMP3Handle = fopen($fMP3Source, 'r');
  12.  
  13.  
  14. $thisLine = fgets($fMP3Handle);
  15. $thisLine = fgets($fMP3Handle);
  16. $arrMP3 = explode(";", $thisLine);
  17.  
  18.  
  19. for($i=0;$i<count($arrMP3);$i++)
  20. {
  21.     echo $arrMP3[$i] . "<br>";
  22. }
  23.  
Viewed in the browser it looks fine. But then I write to the file:

Expand|Select|Wrap|Line Numbers
  1. $currentLine = 0;
  2. $lineCount = count($arrMP3);
  3. while($currentLine < $lineCount)
  4. {
  5.     fwrite($fTargetHandle, $arrFile[$currentLine]);
  6.     if(trim($arrFile[$currentLine]) == "</itunes:owner>")
  7.     {
  8.         fwrite($fTargetHandle, "       <item>\r\n");
  9.         fwrite($fTargetHandle, "           <title>" . $title . "</title>\r\n");
  10.         fwrite($fTargetHandle, "           <link>" . $path . $filename . "</link>\r\n");
  11.  
  12. // etc
  13. // etc
  14.         fwrite($fTargetHandle, "       </item\r\n");
  15.     }    
  16.     $currentLine++;
  17. }
I have spaces between every character
Mar 19 '08 #5
satas
82
It seems that variables $title, $path and $filename have not been initialized.
Mar 20 '08 #6
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
Mar 21 '08 #7
satas
82
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?
Mar 24 '08 #8
hsriat
1,654 Expert 1GB
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.
Mar 24 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mark | last post by:
Hello, I have been searching for a reason for this behavior but no solution so I figured I would ask. Basically I have a web page that displays data in a database to the user for...
6
by: William Krick | last post by:
I have a string containing concatenated ascii "records" that are each terminated by '\n'. For testing purposes, I construct sample data like this... $mystring =...
8
by: Riddler | last post by:
Anyone have an idea about how I could go about writing a function that would simply insert a character inbetween each character in a string? For instance: HELLO WORLD would turn into: H...
5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
5
by: Jonathan Ng | last post by:
Hi, I was wondering if there was a way to include the white spaces in a string. Currently, I am using: scanf("%s", &input); However, this doesn't include the 'space' character or any other...
5
by: FFMG | last post by:
Hi, I need the php equivalent of explode in one of my app. I read a very big file and "explode" each line to fill a structure. The reading of the file, 19Mb, (I will also need to streamline...
4
by: Joe | last post by:
I have a 'random quotes' plugin that I use which reads tab delimited quotes from multiple text files in a directory, and then randomly displays one. Each text file contains multiple lines, each...
15
by: DanielJohnson | last post by:
I am writing a program in which I am removing all the spaces from the string. I thought that I could do it two ways. One was parsing the string character by character and copying onto another...
4
by: micronack | last post by:
{NOTE . = space} I made a script to allow me to edit my webpages online through a text area in my cgi script but I've run into a problem. For whatever reason the text area seems to put spaces in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.