473,775 Members | 3,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Explode inserts spaces between each character in string

3 New Member
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 2496
bonski
53 New Member
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 New Member
try using the trim($variable) ; function to trim out all the spaces.
Mar 19 '08 #3
ronverdonk
4,258 Recognized Expert Specialist
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
need2know2
3 New Member
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 New Member
It seems that variables $title, $path and $filename have not been initialized.
Mar 20 '08 #6
need2know2
3 New Member
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 New Member
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 Recognized Expert Top Contributor
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
2911
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 updates/deletion/addition. For each data field ont he web page I put the data from the database in the field name with teh field name and data seperated by a |. ex: address|35 Elm St.
6
23939
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 = "a|b|c|d\n"."e|f|g|h\n"."i|j|k|l\n"."m|n|o|p\n"; I want to use explode() to put the records into an array like this...
8
21688
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 E L L O W O R L D
5
31179
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 will split myString up using the delimiter of 1 space so that
5
36886
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 white spaces. Is there a way I can include the 'space' character rather than skip in.
5
3463
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 the way I read each line I guess), takes about 10 seconds. But when I 'explode' each line the process takes about 140 seconds. This is what I have tried so far...
4
2837
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 listing a person and a quote, separated by a tab, and each file is based around a topic. I use fopen and fread to suck in all the text, and then explode to separate into an array of names and quotes. It is working fine with one small exception. When...
15
15628
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 output string. But this was trivial. The other option is to use pointers and shift all the characters after the space by one space to the left. I did this program using pointers and then using array too and I get segmentation fault. What is going...
4
2300
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 front of each line {except for the first} so, line one line two line three becomes line one .line two .line three
0
9455
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10272
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9917
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8941
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6719
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5363
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5487
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4020
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3613
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.