Results 1 to 5 of 5

Thread: capturing the first word in a string

  1. #1
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default capturing the first word in a string

    How would you go about capturing the first word in a string or variable as a string. For example how would you take

    $string = "this is text"; and get the first word out of it so that
    $string_first will equal "this"; because "this" is the first word?

    Thanks

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Code:
    $text=preg_replace('/\W.*/','',$text);
    echo "$text";
    works.

    Code:
    $text=preg_split('/[\{.*?\}]/',$text);
    echo "$text[0]";
    will get the first match of everything between { and } and assign it to the array $text.
    Last edited by james438; 12-25-2007 at 01:24 AM.

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    $words = explode(' ', $text);
    $first_word = $words[0];
    $text=preg_split('/[\{.*?\}]/',$text);
    echo "$text[0]";
    If you want to interpolate a complex expression into a double-quoted string you must use braces ({}) to separate it from the rest of the string:
    Code:
    echo "{$text[0]}";
    However, in both of the examples above the double quotes are merely a waste of resources.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Jan 2007
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey View Post
    Code:
    $words = explode(' ', $text);
    $first_word = $words[0];
    If you want to interpolate a complex expression into a double-quoted string you must use braces ({}) to separate it from the rest of the string:
    Code:
    echo "{$text[0]}";
    However, in both of the examples above the double quotes are merely a waste of resources.
    I would prefer the first one because it is faster than regular expression.
    Last edited by rajug; 12-26-2007 at 04:54 AM.

  5. #5
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    I really need to give better examples when I ask a question. I was creting a program (finished now) where a web designer could enter code into a form to create new style scripts. The form will list the style scripts already in the database so that they can be edited or deleted. Next to each style scrpit is a sample text demonstrating what the CSS would look like when applied to a portion of text.

    The script is really only helpful for beginner to intermdiate users and is good as a reference page. I ended up using
    Code:
    $style1=preg_split('/[\{\}]/',$style);
    to apply that which is within the curly brackets to the sample text.

    I have not decided whether to modify it to allow multiple styles to be applied to a single piece of text. For example whether style 1 and style 2 should be applied to "sample text" or whether all of the anchor properties should be applied to a single piece of text. So far it all works fine and I have not run into any problems yet.

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
  •