Jump to content

[SOLVED] trying to explode $count++; from a string!!! but it doesn't work! HELP


samoi

Recommended Posts

Hello guys.

 

I have a .txt file contains info like

779.   TEL:******* [person] Date:2010 07 15, 17:26:45
Hello

this a sample of one row in the .txt file contains the number of the message, the phone# , the persons name, date and time, and the message.

 

I want all the rows with out numbering so that I can do what I want with it. so I tried.

 

<?php
$fcontent = file('./msgs.txt');
$count = 1;
for($i=0; $i<sizeof($fcontent); $i++) { 
  $plus = $count++;
      $line = trim($fcontent[$i]); 
      //$lline = trim($line[$plus]);
      $arr = explode($plus. ".   ", $line); 
      $sam = implode("", $arr);
      echo $sam;
      echo '<br />';
      }

?>

 

But it still prints out the same rows as in the .txt file

 

779.   TEL:******* [person] Date:2010 07 15, 17:26:45
Hello

 

 

any help will be appreciated  :)

as far as why your current solution doesn't work, my first guess would be that if your file starts at 1.  , well you start your counter at 1 and increment it before your explode/implode so it's always off by one so nothing actually gets exploded.  Or it does, but since the delimiter doesn't match in the explode, the whole line gets put into one element.

as far as why your current solution doesn't work, my first guess would be that if your file starts at 1.  , well you start your counter at 1 and increment it before your explode/implode so it's always off by one so nothing actually gets exploded.  Or it does, but since the delimiter doesn't match in the explode, the whole line gets put into one element.

 

 

Thanks for helping,

 

but I used your first help nothing happened, i used your other help and set $count = 0; but that doesn't work either!

 

I will go and read about preg_replace(), but looking forward your help!

 

Thank you

I'd do this

$fcontent = file('./msgs.txt');
foreach($fcontent as $line){ 
// explode on the tab separating line number from the line contents 
$line_exp=explode("\t", $line);   
echo $line_exp[1]."<br>";  
// $line_exp[0] would be the number in your file
}

but I used your first help nothing happened, i used your other help and set $count = 0; but that doesn't work either!

 

Can you show how you implemented it?  It should look something like this:

 

$fcontent = file('./msgs.txt');
// you can use your for loop but this is what foreach loops are really there for...
foreach ($fcontent as $line) {
   $line = preg_replace('~^\d+\.\s+~','',$line);
   echo $line . "<br/>";
}

 

taquitosensei's code might work for you, but if it does, it would be a lucky shot. It would only work if that's actually a tab in there. The \s in my preg_replace matches tabs or spaces.

but I used your first help nothing happened, i used your other help and set $count = 0; but that doesn't work either!

 

Can you show how you implemented it?  It should look something like this:

 

$fcontent = file('./msgs.txt');
// you can use your for loop but this is what foreach loops are really there for...
foreach ($fcontent as $line) {
   $line = preg_replace('~^\d+\.\s+~','',$line);
   echo $line . "<br/>";
}

 

taquitosensei's code might work for you, but if it does, it would be a lucky shot. It would only work if that's actually a tab in there. The \s in my preg_replace matches tabs or spaces.

 

Maaaaaaan you're awesome!

 

but can you tell me what are the symbols there doing ? and where what they're called, I need to learn dealing with them.

'~^\d+\.\s+~' is a regular expression.  Basically it says to start at the beginning of the line, match 1 or more digits, followed by a dot, followed by one or more spaces, and replace it with '' (nothing).

 

:) man I really appreciate it.

 

Do you know a good tutorial about this those regular expressions?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.