4

I am using a regular expression from although this only extracts the text inside of the brackets, I want to completely remove it:

if( preg_match( '!\(([^\)]+)\)!', $text, $match ) )
    $text = $match[1];

E.g I have: my long text string (with another string)

How can I get:

$var1 = "my long text string";
$var2 = "with another string";
1
  • 1
    What should happen if there's something after the closing parentheses? Commented Mar 12, 2013 at 11:06

7 Answers 7

12
// This is all you need
<?php $data = explode('(' , rtrim($str, ')')); ?>

Example:

<?php
$str = 'your long text string (with another string)';
$data = explode('(' , rtrim($str, ')'));    

print_r($data);


// output 

// Array
// (
//     [0] => my long text string 
//     [1] => with another string
// )
// profit $$$$

?>
Sign up to request clarification or add additional context in comments.

1 Comment

Good solution. However, take note that you need to convert the end result from array to a string if that is what you want like OP.
5
$data = preg_split("/[()]+/", $text, -1, PREG_SPLIT_NO_EMPTY);

2 Comments

I believe you'd have to escape the ( and )'s, though.
not inside the [ ] because inside [ ], all special characters are normal :p. Take a look at : sandbox.onlinephpfunctions.com/code/…
1

You could use the code below. But keep in mind that you do need some extra checking to see if there really is an $out[0][0] and $out[0][1]

    <?php
    $string = "my long text string (with another string)";
    preg_match_all("/(.*)\((.*)\)/", $string, $out, PREG_SET_ORDER);
    print_r($out);
    /*
    Array
    (
            [0] => Array
                    (
                            [0] => my long text string (with another string)
                            [1] => my long text string 
                            [2] => with another string
                    )

    )
    */

    $var1 = $out[0][1];
    $var2 = $out[0][2];
    //$var1 = "my long text string";
    //$var2 = "with another string";
    ?>

Comments

1

I am not so good in regular expression, but you can try this.....

$exp=explode("(", $text);
$text1=$exp[0];
$text2=str_replace(array("(",")"), array('',''), $exp[1]);

1 Comment

Nice idea thanks, although I think I would prefer to use the trim instead of str_replace
1
'([^\)]+)\(([^\)]+)\)'

Just remove the !-chars and add a another variable field (the name for brackets-area?) and its ready :)

http://www.solmetra.com/scripts/regex/index.php is worth knowing to get some testing quickly done!

Comments

1

this is a very detailed code... you can do it shorter...

<?php
$longtext = "my long text string (with another string)";
$firstParantheses = strpos($longtext,"(");

$firstText = substr($longtext,0,$firstParantheses);

$secondText = substr($longtext,$firstParantheses);

$secondTextWithoutParantheses = str_replace("(","",$secondText);
$secondTextWithoutParantheses = str_replace(")","",$secondTextWithoutParantheses);

$finalFirstPart = $firstText;
$finalSecondPart = $secondTextWithoutParantheses;

echo $finalFirstPart." ------- ".$finalSecondPart;
?>

Comments

0

Why don't you use this workaround:

$vars = explode('@@', str_replace(array('(', ')'), '@@', $text));

It will replace the brackets with @@ and then explode the text to an array. Additionally you can use array_filter to remove possible empty positions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.