Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 3 votes, 3.00 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-08-2005, 03:29 AM   PM User | #1
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
FUNCTION: TrimByWords()

I wrote this because I was bored and had nothing to do. I also wanted to take the long way about it (at least I think its long?) to consume time. Thought I'd share it with you bre.

PHP Code:
<?php

    $str 
'In the dawn of time man learned to live off of nature around him. Eating nuts, roots, plants, meat and other assorted foods.';

        function 
TrimByWords($str$count) {

          
$trim explode(" "str_replace("  "" "$str));
          
$message "";
          for (
$i 0$i $count$i++) {
            if (
$i == $count) { 
              
$space=""
            } else { 
              
$space=" "
            }
            if (!(
strpos($trim[$i], "."))) {
              
$message .= $trim[$i] . $space;
            } else {
              
$message .= $trim[$i];
              
$i $count;
            }
          }
          
$messag .= " ....";
          return 
$message;

        }
        
        echo 
TrimByWords($str15); // This is set to stop one word after the period, but since there is a period it stops short just to create a trim that makes better sense, and is a different length then other trims.

?>
So that example would output:

Code:
In the dawn of time man learned to live off of nature around him. ....
Where as if it ignored the periods it would output:

Code:
In the dawn of time man learned to live off of nature around him. Eating ....

Last edited by Element; 12-08-2005 at 03:44 AM..
Element is offline   Reply With Quote
Old 12-08-2005, 03:53 PM   PM User | #2
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
I like to have a go at things:
PHP Code:
<?php
function wordswrap($text$words)
{
    
$text preg_split('/\s+/'$text); // No weird spaces, tabs & newlines.
    
if(count($text) <= $words)
    {
        return 
$text;
    }
    
$ret implode(' 'array_slice($text0$words));
    if(
in_array($ret{strlen($ret) - 1}, array('.''!''?'':'';'','))) // Not only .
    
{
        
$ret .= ' ' $text[$words];
    }
    return 
$ret '...';
}

$text 'Hello, I\'m new! What\'s going on?';

print 
wordswrap($text6);
?>
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.
marek_mar is offline   Reply With Quote
Old 12-08-2005, 04:01 PM   PM User | #3
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
Quote:
Originally Posted by marek_mar
I like to have a go at things:
PHP Code:
<?php
function wordswrap($text$words)
{
    
$text preg_split('/\s+/'$text); // No weird spaces, tabs & newlines.
    
if(count($text) <= $words)
    {
        return 
$text;
    }
    
$ret implode(' 'array_slice($text0$words));
    if(
in_array($ret{strlen($ret) - 1}, array('.''!''?'':'';'','))) // Not only .
    
{
        
$ret .= ' ' $text[$words];
    }
    return 
$ret '...';
}

$text 'Hello, I\'m new! What\'s going on?';

print 
wordswrap($text6);
?>
Nice, I like your character safe-guard.

I caught a few problems with mine I think.

1) What if they set the trim too 20 and the period is in the 6th word?
2) What is spaces were alt spaces, or any other method of a space?
3) What if words didn't contain spaces and its nessessary to trim that word?
Element is offline   Reply With Quote
Old 12-08-2005, 04:07 PM   PM User | #4
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
1) Nothing. If the last character isn't one of those chars nothing else will happen.
2) You don't really need them in the preview.
3) You break the string at spaces too. It would work the same way with the exception of breaking at newlines and tabs aswell.
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.
marek_mar is offline   Reply With Quote
Old 12-08-2005, 04:11 PM   PM User | #5
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
Quote:
Originally Posted by marek_mar
1) Nothing. If the last character isn't one of those chars nothing else will happen.
2) You don't really need them in the preview.
3) You break the string at spaces too. It would work the same way with the exception of breaking at newlines and tabs aswell.
I think with question number one it would stop at the 6th character. Because its looking for a period to stop at as well as finding the trim limit.

Yours might be best to use I think. (For anyone reading.) Unless your strings to trim would work with mine.
Element is offline   Reply With Quote
Old 12-08-2005, 04:14 PM   PM User | #6
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
It doesn't "look" for anything. There are no loops in my code.
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.
marek_mar is offline   Reply With Quote
Old 12-08-2005, 05:09 PM   PM User | #7
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
No no, I said I found problems in my method.
Element is offline   Reply With Quote
Old 12-08-2005, 05:27 PM   PM User | #8
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
I didn't see that coming. I didn't think you'd be asking yourself questions.
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.
marek_mar is offline   Reply With Quote
Old 12-08-2005, 05:42 PM   PM User | #9
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
Quote:
Originally Posted by marek_mar
I didn't see that coming. I didn't think you'd be asking yourself questions.

Well now you know... I talk to myself! But the questions were mainly so other people the plan on looking at the code know.
Element is offline   Reply With Quote
Old 12-09-2005, 06:54 AM   PM User | #10
mindlessLemming
Senior Coder

 
Join Date: Oct 2003
Location: Australia
Posts: 1,963
Thanks: 0
Thanked 0 Times in 0 Posts
mindlessLemming is an unknown quantity at this point
... or … ?

Guys, you both missed something there.

... !== …

The first one is three periods. The second one is a single character, an ellipsis; the typographical symbol intended to represent the ommiting of a section (or the remainder) of a piece of text.

Feel free to replace your dots with & #8230; [minus the space] if that's what you really meant
__________________

I take no responsibility for the above nonsense.


Left Justified

Last edited by mindlessLemming; 12-09-2005 at 06:58 AM..
mindlessLemming is offline   Reply With Quote
Old 12-09-2005, 08:25 AM   PM User | #11
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
Quote:
Originally Posted by mindlessLemming
Guys, you both missed something there.

... !== …

The first one is three periods. The second one is a single character, an ellipsis; the typographical symbol intended to represent the ommiting of a section (or the remainder) of a piece of text.

Feel free to replace your dots with & #8230; [minus the space] if that's what you really meant
... Don't even know what you mean. Both ways work fine.
Element is offline   Reply With Quote
Old 12-09-2005, 02:55 PM   PM User | #12
gsnedders
Senior Coder

 
gsnedders's Avatar
 
Join Date: Jan 2004
Posts: 2,340
Thanks: 1
Thanked 7 Times in 7 Posts
gsnedders will become famous soon enough
Quote:
Originally Posted by Element
... Don't even know what you mean. Both ways work fine.
... is not the same as

The first is three full stops; it has no special meaning.

The second is an ellipsis; it indicates something has been omitted.

Using 3 full stops is grammatically incorrect; however, using an ellipsis is correct.
__________________
Geoffrey Sneddon
gsnedders is offline   Reply With Quote
Old 12-09-2005, 04:18 PM   PM User | #13
marek_mar
Sensei


 
Join Date: Aug 2003
Location: One step ahead of you.
Posts: 2,815
Thanks: 0
Thanked 3 Times in 3 Posts
marek_mar is on a distinguished road
It is just a new char acteradded for an unknown reason (for me).
"The something being omitted" is (...) in my language.
It is still grammatically correct as an ellipsis is three dots.
__________________
I'm not sure if this was any help, but I hope it didn't make you stupider.

Experience is something you get just after you really need it.
PHP Installation Guide Feedback welcome.
marek_mar is offline   Reply With Quote
Old 12-09-2005, 08:31 PM   PM User | #14
Element
Regular Coder

 
Element's Avatar
 
Join Date: Jul 2004
Location: Lynnwood, Washington, US
Posts: 855
Thanks: 2
Thanked 2 Times in 2 Posts
Element is an unknown quantity at this point
Quote:
Originally Posted by marek_mar
It is just a new char acteradded for an unknown reason (for me).
"The something being omitted" is (...) in my language.
It is still grammatically correct as an ellipsis is three dots.
H is correct, and infact its four dots to show an omitted string parse, three dots is actually a delay where as four is a delayed stop

For example.

"I don't like eggs... they give me gas."
Or:
"I don't like eggs, they give me gas...."

The HTML entity for the three dots is just a simple way to do it, alowing less space between dots to show it as a single character.

Obviously typing has nothing to do with grammar, three dots by hand or HTML, same concept. When writing there is no special way of doing dots, no its just three dots.
Element is offline   Reply With Quote
Old 12-09-2005, 08:54 PM   PM User | #15
gsnedders
Senior Coder

 
gsnedders's Avatar
 
Join Date: Jan 2004
Posts: 2,340
Thanks: 1
Thanked 7 Times in 7 Posts
gsnedders will become famous soon enough
Quote:
Originally Posted by Element
When writing there is no special way of doing dots, no its just three dots.
On OS X you can quite simply do Alt + ; to get an ellipsis…
__________________
Geoffrey Sneddon
gsnedders is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:45 PM.


Advertisement
Log in to turn off these ads.