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: Trim question

  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post

    Trim question

    I would like to trim part of a string which looks like this:

    $input = "This part should stay (Bracket) (Bracket2) (Bracket3)";

    $output = "This part should stay";


    Second and third brackets do appear sometimes and sometimes not.

    I need the part before the first bracket and leave the space char out.

    How can I do this?

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Those are parenthesis, not brackets.

    ... and not sure what "leave the space char out" means ...

    PHP Code:
    <?php
    $input 
    "This part should stay {Bracket} {Bracket2} (Parenthesis1) (Parenthesis2)";

    $pattern "(\([^\)]*\))";
    $output=eregi_replace($pattern,"",$input);
    echo 
    $output;
    ?>

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post
    Hi mlseim,

    Sorry for "brackets", had no sleep for some time
    Regular expressions again, need to work on them...

    Leave the space char out:

    in your code, $output is generated with a single space char at the end:

    Code:
    $output = "This part should stay ";
    it probably needs something like this:

    Code:
    $output = substr($output, 0, -1);

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    So you did get it to work?
    or you still need to get the last space removed?

    not sure if this thread is finished.

  • #5
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post
    Hi,

    your code works as expected. I managed to remove the last space with the code I wrote. Thank you very much for your help.


  •  

    Posting Permissions

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