samoi Posted April 2, 2009 Share Posted April 2, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/152279-solved-trying-to-explode-count-from-a-string-but-it-doesnt-work-help/ Share on other sites More sharing options...
.josh Posted April 2, 2009 Share Posted April 2, 2009 instead of the explode and implode, do this: $line = preg_replace('~^\d+\.\s+~','',$line); Quote Link to comment https://forums.phpfreaks.com/topic/152279-solved-trying-to-explode-count-from-a-string-but-it-doesnt-work-help/#findComment-799695 Share on other sites More sharing options...
.josh Posted April 2, 2009 Share Posted April 2, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/152279-solved-trying-to-explode-count-from-a-string-but-it-doesnt-work-help/#findComment-799696 Share on other sites More sharing options...
samoi Posted April 2, 2009 Author Share Posted April 2, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/152279-solved-trying-to-explode-count-from-a-string-but-it-doesnt-work-help/#findComment-799705 Share on other sites More sharing options...
taquitosensei Posted April 2, 2009 Share Posted April 2, 2009 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 } Quote Link to comment https://forums.phpfreaks.com/topic/152279-solved-trying-to-explode-count-from-a-string-but-it-doesnt-work-help/#findComment-799710 Share on other sites More sharing options...
.josh Posted April 2, 2009 Share Posted April 2, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/152279-solved-trying-to-explode-count-from-a-string-but-it-doesnt-work-help/#findComment-799719 Share on other sites More sharing options...
samoi Posted April 2, 2009 Author Share Posted April 2, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/152279-solved-trying-to-explode-count-from-a-string-but-it-doesnt-work-help/#findComment-799757 Share on other sites More sharing options...
.josh Posted April 2, 2009 Share Posted April 2, 2009 '~^\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). Quote Link to comment https://forums.phpfreaks.com/topic/152279-solved-trying-to-explode-count-from-a-string-but-it-doesnt-work-help/#findComment-799764 Share on other sites More sharing options...
samoi Posted April 2, 2009 Author Share Posted April 2, 2009 '~^\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? Quote Link to comment https://forums.phpfreaks.com/topic/152279-solved-trying-to-explode-count-from-a-string-but-it-doesnt-work-help/#findComment-799789 Share on other sites More sharing options...
.josh Posted April 3, 2009 Share Posted April 3, 2009 see the stickies in this forum: http://www.phpfreaks.com/forums/index.php/board,43.0.html Quote Link to comment https://forums.phpfreaks.com/topic/152279-solved-trying-to-explode-count-from-a-string-but-it-doesnt-work-help/#findComment-800055 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.