View Full Version : Trim question

02-04-2007, 11:39 PM
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?

02-05-2007, 02:46 AM
Those are parenthesis, not brackets.

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

$input = "This part should stay {Bracket} {Bracket2} (Parenthesis1) (Parenthesis2)";

$pattern = "(\([^\)]*\))";
echo $output;

02-05-2007, 06:35 PM
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:

$output = "This part should stay ";

it probably needs something like this:

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

02-06-2007, 03:56 PM
So you did get it to work?
or you still need to get the last space removed?

not sure if this thread is finished.

02-06-2007, 05:14 PM

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