Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts

    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 04:44 AM.

  • #2
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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.

  • #3
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    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?

  • #4
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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.

  • #5
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    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.

  • #6
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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.

  • #7
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    No no, I said I found problems in my method.

  • #8
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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.

  • #9
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    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.

  • #10
    Senior Coder
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    1,963
    Thanks
    0
    Thanked 0 Times in 0 Posts

    ... 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
    Last edited by mindlessLemming; 12-09-2005 at 07:58 AM.

    I take no responsibility for the above nonsense.


    Left Justified

  • #11
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    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.

  • #12
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    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.

  • #13
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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.

  • #14
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    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.

  • #15
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    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…


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •