Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: how to get only the words between a parenthesis () and clear everything else

Tags:

php

I have an array with some info. For example:

(writer) &

or

with (additional dialogue)

I want to clean this so I only get the text between the parenthesis () and clear everything else

result:

writer

or

additional dialogue

like image 336
Jonathan Avatar asked Apr 09 '09 19:04

Jonathan


1 Answers

The easiest way will be with a regular expression:

preg_match_all('/\((.*?)\)/', $input, $matches);

$matches[1], $matches[2], etc will contain everything that was between parentheses in $input. That is, $matches[1] will have whatever was between the first set of parentheses, and so on (to handle cases with multiple sets).

like image 198
Chad Birch Avatar answered Sep 20 '22 13:09

Chad Birch