4 Replies - 1902 Views - Last Post: 23 September 2012 - 07:02 AM Rate Topic: -----

#1 mshadow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 03-July 12

error in php explode

Posted 22 September 2012 - 08:02 AM

hi forum
this is my code
<?php
$data=file_get_contents('./people.txt',true);
echo "$data"."<br>";
$newdata=str_replace(" ",";",$data);
echo "$newdata"."<br>";
$file=explode(";",$newdata);
$len=count($file);
echo "$len"."<br>";
for($i=0;$i<$len;$i++)
{
 echo "$file[$i]"."<br>";
}

?>


which opens this text file(link given)
n the out put i'm getting is
how are you where are you i'm here and you
how;are;you where;are;you i'm;here and;you
7
how
are
you where
are
you i'm
here and
you

the last word of a line and the first word of the next line is being treated as a single word...is there any way to join the lines avoiding line breaks...

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: error in php explode

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2934
  • View blog
  • Posts: 10,139
  • Joined: 08-August 08

Re: error in php explode

Posted 22 September 2012 - 08:15 AM

Replace line breaks with your delimiter. In this case, semicolons.
Was This Post Helpful? 0
  • +
  • -

#3 mshadow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 03-July 12

Re: error in php explode

Posted 22 September 2012 - 08:31 PM

View PostCTphpnwb, on 22 September 2012 - 08:15 AM, said:

Replace line breaks with your delimiter. In this case, semicolons.

i tried that by str_replace("/n",";",$data)
but the lines were separated by two semicolons
;;
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: error in php explode

Posted 23 September 2012 - 04:21 AM

That's not the content of the text file you provided. This is what you provided:

fdxgtfcvghfdvt ctftrd
hgfvtf bytfvbtf


How about the file you are ACTUALLY using, and what you expect the final output to look like?
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2934
  • View blog
  • Posts: 10,139
  • Joined: 08-August 08

Re: error in php explode

Posted 23 September 2012 - 07:02 AM

View Postmshadow, on 22 September 2012 - 11:31 PM, said:

View PostCTphpnwb, on 22 September 2012 - 08:15 AM, said:

Replace line breaks with your delimiter. In this case, semicolons.

i tried that by str_replace("/n",";",$data)
but the lines were separated by two semicolons
;;

Is /n a line break in your system? It's not in any system I'm aware of. In Unix based systems it's \n
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1