SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    Jan 2002
    Location
    Scotland, UK
    Posts
    530
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Making a simple news feed for my site in PHP

    I was wondering how to read a certain amount of characters from a string and display only the first 20 of them but removing any half words at the end of the string.

    Eg:

    mainstring ="Hello and welcome to yet another script"

    should make :

    choppedstring = "Hello and welcome to"

    If it landed on half a word at the end if would backtrack and remove it from the new string :

    oldstring = "Hello and welcome to y"
    newstring = "Hello and welcome to"

    I know Im not explaining myself very well but hopefully the examples will help show what I mean

    Thanks

  2. #2
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want it to only show whole words then you could try this:
    PHP Code:
    <?
    $string 
    "Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah.";
    $words explode(" "$string);
    for (
    $i 0$i <= 5$i++) {
      echo 
    $words[$i]. " ";
    }
    ?>
    The only way I know how to do it so it will only show the first 20 charaters is to use if statements for it like
    PHP Code:
    <?
    $string 
    "Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah.";
    for (
    $i 0$i <= 20$i++) {
      if (
    $string[20] == " ") {
        echo 
    $string[19];
      } else {
        echo 
    $string[$i];
      }
    }
    ?>
    But that doesn't really work well cause you would have to have it find the word right before the cutopff word.
    - the lid is off the maple syrup again!

  3. #3
    SitePoint Evangelist
    Join Date
    Jan 2002
    Location
    Scotland, UK
    Posts
    530
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, is there a command similar to STRCOPY where I can copy so many chars from a string into another string?

  4. #4
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could use:

    PHP Code:
    <?php
    $old 
    'thirty and many many more characters, too many to count';
    $new substr($old020);

    echo 
    $new;
    ?>
    That takes the first twenty characters of the old string and stores it in the variable $new.

  5. #5
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are storing the news in a MySQL database you can select the number of words you want returned in your query:

    Code:
    SELECT SUBSTRING_INDEX(field, ' ', 20) as newstext from table;
    Just change 20 to how many words you want and they will be returned in "newstext"

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  6. #6
    SitePoint Evangelist
    Join Date
    Jan 2002
    Location
    Scotland, UK
    Posts
    530
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by seanf
    If you are storing the news in a MySQL database you can select the number of words you want returned in your query:

    Code:
    SELECT SUBSTRING_INDEX(field, ' ', 20) as newstext from table;
    Just change 20 to how many words you want and they will be returned in "newstext"

    Sean
    nice one!

    I'll have a go at that


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
  •