?
Solved

unwanted spaces in array

Posted on 1999-12-22
6
Medium Priority
?
244 Views
Last Modified: 2010-08-05
here is my code...

$cuswords = file( "cuswords.txt");
$array_size = sizeof($cuswords);
 
 
 for($i=0; $i<$array_size; $i++) {
     $cuswords[$i] = str_replace("\n","",$cuswords[$i]);
 }
 
 /*testing array*/
for($i=0;$i<$array_size;$i++){
 echo $cuswords[$i];}

i want to test this array against strings.... but when i use the test print i see that the values are printed "blah blah blah" while they should be stored as BlahBlahBlah with no spaces...
please help....

The text file is in the form of

blah
blah
blah
blah
blah

if I understand correctly with the $value = file() command each new line is read as a new value into the array

thanx everyone....

Rain..
0
Comment
Question by:Rain
6 Comments
 
LVL 5

Expert Comment

by:aarie
ID: 2300063
I think this has to do with your string replace arguments.
Assuming the text in your txt file is as follows:
"Blah bla bla"
(so on one line separated by spaces)...

Instead of "\n" you should then replace just " " with "".

Arjan.
0
 
LVL 5

Expert Comment

by:aarie
ID: 2300065
By the way, if the text in your textfile is arranged in an other way, please post it so we can do some tests ourselves.

Arjan.
0
 

Author Comment

by:Rain
ID: 2300739
Edited text of question.
0
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

 
LVL 2

Expert Comment

by:freshmeat
ID: 2302249
what about:
$cuswords[$i] = str_replace('\n','',$cuswords[$i]);


?
coz the length of $cuswords[$i]is only 1
 
0
 

Expert Comment

by:hackersmile
ID: 2307079
$cuswords[$i] = str_replace('\n','',$cuswords[$i]);
would be wrong. [ you guess why : hint ' != " ]
What I think is either :
1. you have some DOS text file, so the line delimiter is \r\n
you replace only the \n. Then when outputed in HTML(I guess)
you see the \r as a space. Try replacing "\r\n"
2 . or , the echo also sends a \n.

But, I don't see the meaning. What are you trying to compare?
the whole text in the file? Then just do
<?php
$all = file ('mytextfile.txt');
$all = implode ("",$all);
$all = str_replace ("\n","",$all);
Header("Content-Type: text/plain");
echo "-->$all<--" ; //to see if it is the expected string;
?>
0
 
LVL 2

Accepted Solution

by:
maxkir earned 80 total points
ID: 2309998
$cuswords = file( "cuswords.txt");
$array_size = sizeof($cuswords);
                                     
                                       
 for($i=0; $i<$array_size; $i++)
{
    $cuswords[$i] = trim(str_replace("\n","",$cuswords[$i]));
}
                                     
     
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question