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.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Nov 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    regex to truncate a string

    Can any regex experts out there help me cut off a string after ten words?

    For example I have string such as:
    one two three four five six seven eight nine ten eleven twelve thirteen
    (note has no commas or punctuation, only spaces seperate the words)

    I'd like to use an ereg_replace function (or something like that) and a regular expression to get rid of everything after the word ten in this string

    Is there a way to match everything after the first 10 spaces and get rid of it?

    Thanks

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,455
    Thanks
    8
    Thanked 1,084 Times in 1,075 Posts
    I found this example on the internet.
    I like this example because you can pick how many words to truncate:

    PHP Code:
    <?php

    $string
    ="one two three four five six seven eight nine ten eleven twelve thirteen";

    echo 
    "The answer is: <b>".word_trim($string,10)."</b>";

    function 
    word_trim($string$count$ellipsis FALSE){
      
    $words explode(' '$string);
      if (
    count($words) > $count){
        
    array_splice($words$count);
        
    $string implode(' '$words);
        if (
    is_string($ellipsis)){
          
    $string .= $ellipsis;
        }
        elseif (
    $ellipsis){
          
    $string .= '&hellip;';
        }
      }
      return 
    $string;
    }

    ?>
    Here's the output:

    The answer is: one two three four five six seven eight nine ten

    NOTE:
    I forgot to mention, if "$ellipses = TRUE" it puts ... at the end.
    like, "eight nine ten ... "


    .
    Last edited by mlseim; 12-20-2006 at 03:00 AM.

  • #3
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Use preg_split() and then use the array_pop() function in a for loop to get rid of the last elements past 10.
    PHP Code:
    <?php
    $string 
    "one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen";
    echo 
    "The string is:<br>$string<br><br>";
    $array preg_split('/[\s]+/',$string);
    $total count($array) - 10;
    for(
    $i 0$i $total$i++) // we want to pop the last element of the array until we get to the count of the array minus 10
    {
        
    array_pop($array); // takes off last element in array until for loop condition is met

    echo 
    "The new string is:<br>";
    foreach(
    $array as $key// grabs all array values and echos them
    {
        echo 
    "$key ";
    }
    ?>
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #4
    New Coder
    Join Date
    Nov 2006
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for the info guys. i went on a brief journey around the net to look up some stuff also and here's what i was able to cobble together. my original impression that using regex functions to accomplish the string shortening would be easier and more elegant is probably not the case. anyway, for what it's worth:

    regex pattern to match first 10 words: "^([a-zA-Z0-9]* ){0,9}[a-zA-Z0-9]*";
    ([a-zA-Z0-9]* ) - matches a substring of letters and nums of any length followed by a space ' '
    {0,9} - does this from 0 - 9 times
    [a-zA-Z0-9]* - matches the 10th substring of letters and nums (note: doesn't caputure a white space if there is one)

    then you can put this in a function like so:
    function chop_str($str, $items){
    $pattern = "^([a-zA-Z0-9]* ){0,".($items-1)."}[a-zA-Z0-9]*";
    $matches = array();
    if(eregi($pattern, $str, $matches)){
    return rtrim($matches[0]); //possible to capture extra white space at end so trim it off
    } else {
    return $str;
    }
    }

    $matches[0] contains the first match of the regex
    you can read more about ththe eregi function on the php.net website
    Last edited by dschmierer; 12-20-2006 at 04:10 AM.

  • #5
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Hmm I think my solution is a little better since it catches any amount of white spaces. Its also a little more efficient (less code). If you wanted to, you could put it in a function like so
    PHP Code:
    <?php
    $string 
    "one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen";
    trimString($string);
    function 
    trimString($s)
    {
        
    $array preg_split('/[\s]+/',$s); // assigns each word to array cell
        
    $total count($array) - 10;
        for(
    $i 0$i $total$i++) // we want to pop the last element of the array until we get to the count of the array minus 10
        
    {
            
    array_pop($array); // takes off last element in array until for loop condition is met
        

        foreach(
    $array as $key)
        {
            echo 
    "$key ";
        }
    }
    ?>
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #6
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    array_splice() should be better than the for loop, implode() should be better than the foreach() loop.
    PHP Code:
    <?php
    $string 
    "one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen";
    print 
    trimString($string);
    function 
    trimString($s)
    {
        
    $array preg_split('/[\s]+/',$s); // assigns each word to array cell
        
    array_splice($arraycount($array) - 10); // takes off last 10 elements? Shouldn't this just leave the first 10?
        
    return implode(' '$array);
    }
    ?>
    Last edited by marek_mar; 12-20-2006 at 06:03 AM.
    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
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Hmm that is taking all of them off except the number of count - 10. Reversing it making it 10-count returns the correct number of words
    Code:
    array_splice($array, 10 - count($array));
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #8
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    It's easier to match the first ten words and keep them than to match everything else and discard it:

    PHP Code:
    $str 'one two three four five six seven eight nine ten eleven twelve thirteen fourteen';
    preg_match('/\A(\w+\s+){0,10}/xms'$str$match);
    echo 
    $match[0]; 
    edit: oops, missed the post above with nearly the same answer. carry on
    Last edited by ralph l mayo; 12-20-2006 at 05:06 PM.


  •  

    Posting Permissions

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