SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP String Chopping Excerpt

    Hi there,

    Let's say I have a 400 character string... and there's a string that I'm searching for in it (i.e. "#hashtag"). How can I only display the first 50 characters BEFORE and AFTER the #hashtag string?

    Thanks in advanced!

  2. #2
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    Something like this?

    PHP Code:
    <?php

    $string 
    'Lor em ipsum dolor sit #hastag amet, consectetur adipiscing elit. Sed metus augue, mollis non ornare eget, consectetur non eros. In hac habitasse platea dictumst. Duis egestas ultricies iaculis';
    $before 10;
    $after 10;
    $find '#hastag';

    $pos strpos($string$find);

    $start $pos $before// work out where to start
    $length $start $after strlen($find) - 2// work out where to end

    $start max($start0); // make sure we don't start before the beginning of the string
    $length min($lengthstrlen($string)); // make sure we don't end after the end of the string

    $new substr($string$start$length);

    echo 
    $new;

    ?>

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    @Panduola - great reply by immerse, but wouldn't 10 words before and 10 words after the hashtag make a bit more sense?

    Or, isolate and extract the sentence which contains the hashtag?

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2010
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @Immerse thank you very much!

    @Cups ahh! yes! This is actually what I meant to say... 10 words before and after... not characters. How might this look in code?

    Thanks again!

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Might look like this :

    split() the string into words (ie on a space).

    array_search() the #hashtag and return the target key number

    then loop through the array and concat the words before and after the target
    PHP Code:
    $string 'Lor em ipsum dolor sit #hashtag amet, consectetur adipiscing elit. Sed metus augue, mollis non ornare eget, consectetur non eros. In hac habitasse platea dictumst. Duis egestas ultricies iaculis'

    $words split(" "$string);

    // var_dump( $words );

    $tag_index array_search('#hashtag'$words);

    $tgt="";
    foreach(
    $words as $k=>$word){

    if ( (
    $k $tag_index 10)  && ( $k $tag_index 10 ) ){
    $tgt .= $word ' ';
    }

    }

    echo 
    $tgt;

    // Lor em ipsum dolor sit #hashtag amet, consectetur adipiscing elit. Sed metus augue, mollis non 

    There is probably a more efficient way of doing this, but that should get you started. If that string can be 1000s of words, I'd look at splitting on the #hashtag and get the last 10 words from array 1 and the first 10 words from array 2. You could also use join() instead of string concatenation.


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
  •