Professional Web Applications Themes

Split string at word boundary - PHP Development

I want to take a plain text string of arbitrary length and reduce its lenth to a set maximum. However I want the split to be at a word boundary (whitespace) not half way through a word. Is there an easy solution? ChrisJ...

  1. #1

    Default Split string at word boundary

    I want to take a plain text string of arbitrary length and reduce its
    lenth to a set maximum. However I want the split to be at a word
    boundary (whitespace) not half way through a word.

    Is there an easy solution?

    ChrisJ
    ChrisJ Guest

  2. #2

    Default Re: Split string at word boundary

    ChrisJ wrote: 

    Well, define easy ;)

    You could follow the following steps:

    1. specify the max length
    2. have a look at the textpositions: max,max+1
    if one of this characters is a whitespace you just copy max
    characters from the text (keep track of text length)
    if not, you have to find the last whitespace in the text between
    character 0 and character max

    Well you don't really need to check the first condition, you just could
    search for the first whitespace starting at the position max+1 as long
    as max+1 is lower than the length of the text

    Hope that helps

    Regards
    Stefan Rybacki


     
    Stefan Guest

  3. #3

    Default Re: Split string at word boundary

    ChrisJ wrote:
     

    I'm no programmer, but I wanted something like this too, so here's a
    function I hacked together after I read your message. There's probably a
    much easier and more elegant way of doing it, though...

    function trim_text($text,$length,$addellipsis=TRUE) {
    $words = explode(' ',$text);
    $newphrase = '';
    $finished = FALSE;
    while(!$finished) {
    $newphrase .= ' '.array_shift($words);
    if(strlen($newphrase.' '.$words[0]) > $length) { $finished = TRUE; }
    }
    if($addellipsis) { $newphrase .= ' ...'; }
    return $newphrase;
    }

    The $addellipsis bit is because I like to do that if I've truncated the
    text, which is why I do it by default.

    +

    Phil Guest

  4. #4

    Default Re: Split string at word boundary

    Phil wrote: 
    >
    >
    > I'm no programmer, but I wanted something like this too, so here's a
    > function I hacked together after I read your message. There's probably a
    > much easier and more elegant way of doing it, though...
    >
    > function trim_text($text,$length,$addellipsis=TRUE) {
    > $words = explode(' ',$text);
    > $newphrase = '';
    > $finished = FALSE;
    > while(!$finished) {
    > $newphrase .= ' '.array_shift($words);
    > if(strlen($newphrase.' '.$words[0]) > $length) { $finished = TRUE; }
    > }
    > if($addellipsis) { $newphrase .= ' ...'; }
    > return $newphrase;
    > }[/ref]

    may you try this:

    function trim_text($text, $length) {
    //you do not need to test for whitespaces since the text is shorter
    than the given length
    if (strlen($text)<=$length) {
    return $text;
    } else {
    //find the first whitespace (space, tab, ...) starting at the end
    for ($i=min($length,strlen($text)-1);$i>=0;$i--) {
    //check character for whitespace
    if (preg_match("-\W-",$text[$i])) {
    return substr($text,0,$i);
    }
    }
    //if we reach here no whitespace was found
    return substr($text,0,$length);
    }
    }


    this function allows other whitespace than space and it is twice as fast
    as yours.
    And your function also produces a leading space.

    Regards
    Stefan

     
    Stefan Guest

  5. #5

    Default Re: Split string at word boundary

    An even better solution in speed is this:

    function reg_trim_text($text,$length) {
    //test before using a regular expression
    if (strlen($text)<=$length) {
    return $text;
    }
    //using a regular expression
    if (preg_match("-^(.*)[\W][\w]*$-",substr($text,0,$length+1),$res)) {
    return $res[1];
    } else {
    return substr($text,$length);
    }
    }

    Regards
    Stefan
    Stefan Guest

Similar Threads

  1. Split string?
    By Steve Grosz in forum Macromedia ColdFusion
    Replies: 1
    Last Post: June 13th, 10:21 PM
  2. split a word after n-letters (regexp?)
    By Stephan in forum PERL Beginners
    Replies: 3
    Last Post: June 11th, 11:18 AM
  3. How to split a special string...
    By Reply-Via-Newsgroup in forum PHP Development
    Replies: 4
    Last Post: February 3rd, 10:56 AM
  4. new warning in 1.8 from String#split
    By Phil Tomson in forum Ruby
    Replies: 0
    Last Post: October 11th, 06:14 PM
  5. split the string by using the cursor
    By Naren in forum Microsoft SQL / MS SQL Server
    Replies: 3
    Last Post: August 7th, 04:30 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139