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 5 of 5
  1. #1
    mat
    mat is offline
    Regular Coder
    Join Date
    Jul 2002
    Posts
    199
    Thanks
    0
    Thanked 0 Times in 0 Posts

    text - cut off at the nearest space

    To grab only the first 150 characters from a mySQL table field containing text I use the following:

    PHP Code:
    Left(story,115
    .. in my SQL select statement.

    Unfortunately this tends to cut out in the middle of words though which isn't to great.


    Is there a simple-ish using php I make it cut off at the nearest space?

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    if you wanted to grab say the first $x words you could use MySQL's SUBSTRING_INDEX...

    e.g. first 20 words ...
    SELECT SUBSTRING_INDEX( fieldname ,' ', 20 ) ;

    in PHP would probably be messy compared to the above ... something like ??

    (untested and cheesy :: might give you a string between 150 - 164 chars)
    PHP Code:
    <?php
    $pre 
    substr$str ,148 );
    /*assuming most words are less than 15 chars*/
    $ext substr$str 149 15 ); 
    $bits explode' ' $ext ) ;

    echo 
    $pre $bits[0];
    ?>
    though perhaps there is some funky regex to do the same ?
    MySQL would still be the best bet from a performace perspective
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    errr actually ignore that

    this does work & gives a max of 150 chars

    PHP Code:
    <?php
    $pos 
    strrpossubstr$str 150 ) ,' ' ) ;
    echo 
    substr$str $pos );
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #4
    mat
    mat is offline
    Regular Coder
    Join Date
    Jul 2002
    Posts
    199
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I see.. thank you.

  • #5
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    And I also wrote this for someone on here long ago which does basically the same thing except it parses the string and allows you to remove imperfections and attempts to cut the string off at the nearest sentence but under the word limit.

    PHP Code:

    <form name="foo" method="post" action="<?php echo $PHP_SELF ?>">
      <textarea name="input" cols="50" rows="6" wrap="VIRTUAL" id="input"></textarea>
      <br>
      <input type="submit" name="Submit" value="Submit">
    </form>
    <?php

    $input 
    $_POST["input"];

    function 
    process_string($string) {

    // Number of words to return.
    $word_limit 5;

    // Remove line breaks, double spaces, etc from the string
    // You can add to this section to clean up the string
    // before attempting to count the number of words.
    $string str_replace("\n"," ",$string);
    $string str_replace("  "," ",$string);

    // Split the string into an array of words.
    // It is set to split when a single space is found
    $word_array explode(" "$string);

    // Counts the number of elements in the array
    // which should be about how many words were in the string
    $num_of_words count($word_array);

    // Trims the array of words.  Removes all words after
    // what is specified in the $word_limit 
    $word_array_trimmed array_splice ($word_array0$word_limit);

    // Converts the array of words back into a string
    $final_string implode(" ",$word_array_trimmed);

    return 
    $final_string;
    }

    echo 
    "<br><br>" process_string($input);

    ?>
    It determines sentences based on periods being at the end of the sentence however there are some situations that will break that logic such as periods being used other than at the end of a sentence.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster


  •  

    Posting Permissions

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