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
    New Coder
    Join Date
    Jan 2004
    Location
    Australia
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    str_word_count and colons

    I have used the function str_word_count to split a text into words but this function will ignore colons so that a string like this

    ProperName: Fred

    is split into "ProperName" and "Fred"

    I would like the colon included so that the result is

    "ProperName:" and "Fred"

    What would be a good way of doing that?

  • #2
    New Coder
    Join Date
    Jan 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think if you use the explode function the colons will remain (although i never tested it).

    Code:
    $string = 'ProperName: Fred';
    $string_new = explode(" ", $string);
    Hope this helps
    stuartc1
    -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
    www.php-editors.com - PHP Editors Reviewed
    www.phphub.com - PHP Community Hub
    www.php-freelancers.com - PHP Freelance Jobs
    -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The problem with explode() is that you get empty elements in the result array if the string contains words seperated by more than one space. Also, words seperated by line-breaks, tabs etc. are not distinguished either.

    You can perform a split along word boundaries more precisely with preg_split:

    Code:
    $result = preg_split('/\s+/', $string, -1, PREG_SPLIT_NO_EMPTY);
    De gustibus non est disputandum.

  • #4
    New Coder
    Join Date
    Jan 2004
    Location
    Australia
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks I'll try them out and see if they do what I need. The strings I have do form sentences and paragraphs. I just want each word but to include the semicolon (and only the semicolon) if it's used.

  • #5
    New Coder
    Join Date
    Jan 2004
    Location
    Australia
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks to both of you for you help. Mordred you case was much what I was after. I had to delve into the ugly world of reg expr. But

    $psmethod = preg_split('/[\s,.]+/', $string, -1, PREG_SPLIT_NO_EMPTY);

    gave me what I was after


  •  

    Posting Permissions

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