#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    59
    Rep Power
    15
    I am trying to find a space in a string. If I have a sentence like:

    This is a sentence.

    I would like to be able to find the second space form the endand then return that part of the sentence, so if I ran it on that sentnece above it would return "a sentence.", but I won't know what the sentence is.

    Thanks
    Chris

  2. #2
  3. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15
    Chris,

    It can be done easily..
    I am giving a very small example..

    <?

    $string="This is a sentence";

    $array=explode(" ",$string);
    //seperate all the values based on space
    $array_count=count($array);
    // take count of words

    $sentence=$array[$array_count-2]." ".$array[$array_count-1];
    //concatenate last two words..add space in between..

    echo $sentence;

    ?>


    Good Luck!!!


    ------------------
    SR -
    webshiju.com

    "The fear of the LORD is the beginning of knowledge..."

    [This message has been edited by Shiju Rajan (edited July 20, 2000).]
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 1999
    Location
    Annapolis, Maryland US
    Posts
    113
    Rep Power
    16
    I'm not really sure I understand what your're after but...

    <?
    $string="This is a sentence";
    $offset=2; // two words from the end
    $list=split("[[:space:]]", $string);
    for($i=count($list)-$offset; $i<count($list); $i++)
    print"$list[$i] ";
    ?>

    will return "a sentence", or $offset number of words from a string containing $offset-1 spaces in it.

Similar Threads

  1. array explode ( string separator, string string [, int limit])
    By beemee_freeserv in forum HTML Programming
    Replies: 4
    Last Post: February 5th, 2004, 11:56 AM
  2. Finding a string within a string
    By IceHammer in forum PHP Development
    Replies: 1
    Last Post: February 1st, 2004, 01:48 AM
  3. split string with irregular space patern
    By sampah95 in forum PHP Development
    Replies: 1
    Last Post: January 29th, 2004, 12:49 PM
  4. replace space inside a string
    By dave2k in forum PHP Development
    Replies: 4
    Last Post: January 6th, 2004, 01:19 PM
  5. finding the string before a special character
    By sri197 in forum XML Programming
    Replies: 0
    Last Post: December 11th, 2003, 03:57 AM

IMN logo majestic logo threadwatch logo seochat tools logo