Current location: Hot Scripts Forums » Programming Languages » PHP » Remove All Lines Less Than 3 Words Longs


Remove All Lines Less Than 3 Words Longs

Reply
  #1 (permalink)  
Old 09-09-10, 02:19 PM
young_coder young_coder is offline
New Member
 
Join Date: Aug 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Remove All Lines Less Than 3 Words Longs

Dear all,
I want to remove all lines less than 3 words longs so I can stuff the rest into a db.
Can somebody help me?
Thank you in advance
Reply With Quote
  #2 (permalink)  
Old 09-12-10, 09:33 AM
Keith's Avatar
Keith Keith is offline
Community Liaison
 
Join Date: Feb 2004
Posts: 1,232
Thanks: 1
Thanked 11 Times in 11 Posts
What are you working with that you need the lines removed from? A text file? An array?
__________________
The toxic ZCE
Reply With Quote
  #3 (permalink)  
Old 09-15-10, 10:53 PM
bigdaddy75 bigdaddy75 is offline
Newbie Coder
 
Join Date: Aug 2010
Location: Minnesota, USA
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
That is pretty vague. Can you give more details as to what you are trying to do and the code you are working with?
Reply With Quote
  #4 (permalink)  
Old 09-16-10, 02:14 PM
Frement Frement is offline
Newbie Coder
 
Join Date: Jul 2010
Posts: 19
Thanks: 0
Thanked 1 Time in 1 Post
PHP Code:

<?php


show_source
(__FILE__);

$string "Line 1\nLine 2\nLine 3";

$lines explode("\n"$string);

$i 0;
$result;

foreach (
$lines as $line) {
    if (
strlen($line) > 2) {
        
$result[$i] = $line;
        
$i++;
    }
}

echo 
$result;

?>
This will split the given string by \n and loop through the array, resulting in array which contains only lines longer then 2 lines.

You can skip the splitting and insert your own array to the foreach loop if you already have one.

Hope this helps.
Reply With Quote
  #5 (permalink)  
Old 09-16-10, 03:24 PM
Frement Frement is offline
Newbie Coder
 
Join Date: Jul 2010
Posts: 19
Thanks: 0
Thanked 1 Time in 1 Post
I read the first post wrong, heres a version that has word detection, by splitting the line with " " (space).

PHP Code:

<?php


show_source
(__FILE__);

$string "Line 1 has over 3 words.\nLine 2\nLine 3 has more then 3 words.";

$lines explode("\n"$string);

$i 0;
$result;

foreach (
$lines as $line) {
    
$words explode(" "$line);
    if (
count($words) > 2) {
        
$result[$i] = $line;
        
$i++;
    }
}

print_r($result);

?>
Heres a preview: http://www.frement.net/young_coder.php
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove lines which have no specified values? Mythvn PHP 3 07-13-09 09:07 PM
Autolink Script - Link once/keyword cocaine_energy_drink PHP 1 09-22-08 07:19 PM
Autolink Script Tweaks cocaine_energy_drink Script Requests 0 09-21-08 09:07 PM
PHP - Unable to remove items from a file AdrianLewis PHP 5 03-17-06 05:14 AM
need a systex to show first 50 words from a variable loeddie PHP 14 03-24-05 12:38 AM


All times are GMT -5. The time now is 08:20 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.