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

Thread: string trouble

  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    string trouble

    I have this string:

    $old_text = "ert, ert,ert,ert,ert";

    Sometimes there is a space behind the comma, sometimes it's not. How can I test if there is a space behind a comma, and if it is'nt put a space behind it?

    So that $old_text prints:
    ert, ert, ert, ert, ert

    Something like this:

    Search through the string $old_text, and test
    if(there is space behind a comma)
    {do nothing}
    else
    {insert a space behind the comma}

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    183
    Thanks
    0
    Thanked 0 Times in 0 Posts
    $old_text = preg_replace ("/(, ?| ?,)/",", ",$old_text);

    That should make it grammatically correct
    Offtone.com - In the works...

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ...I can't resist... must... use... Regular Expressions

    PHP Code:
    $old_text "ert, ert,ert,ert,ert"
    $new_text preg_replace('/,(?! )/'', '$old_text);
    echo 
    $new_text
    If you're curious, check out the preg_replace function and the Pattern Syntax for lookahead assertions.

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    183
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Mines better

    Mine'll turn this:

    text, text , text ,text

    Into this:

    text, text, text, text

    Offtone.com - In the works...

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You're a genius.

    Mine does exactly what the original poster requested, nothing more or less, no hidden agendas as in your snippet.


  •  

    Posting Permissions

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