A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearPHP: how to get only the words between a parenthesis () and clear everything else - page 1
User InfoPosts
PHP: how to get only the words between a parenthesis () and clear everything else#1
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

posted date: 2009-04-09 11:46:00


Re: PHP: how to get only the words between a parenthesis () and clear everything else#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-09 11:46:01


Re: PHP: how to get only the words between a parenthesis () and clear everything else#3
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).

posted date: 2009-04-09 11:49:00


Re: PHP: how to get only the words between a parenthesis () and clear everything else#4
$matches = array();$num_matched = preg_match_all((/\((.*)\)/U(, $input, $matches);

posted date: 2009-04-09 11:51:00


Re: PHP: how to get only the words between a parenthesis () and clear everything else#5
"The easiest way will be with a regular expression:" ...not if you want the parenthesis to balance! How do you think the OP wants to process "this (is (a) test)"? Your regex matches that as "is (a". Also, it's faster (I think) to do /[^)]*)/ than to do /.*?/ to get text between parenthesis.

posted date: 2009-04-09 12:06:00


Re: PHP: how to get only the words between a parenthesis () and clear everything else#6
If he had specified that there could be nested parentheses I wouldn't have suggested regex. But he seems to be dealing with pretty simple input overall. Writing a parser when a one-line regex will do the same job is overkill.

posted date: 2009-04-09 12:20:00


Re: PHP: how to get only the words between a parenthesis () and clear everything else#7
$string = "this (is (a) test) with (two parenthesis) duh";For a string like this you can use preg_match_all and use implode.$string = "this (is (a) test) with (two parenthesis) duh";$regex = (#\((([^()]+|(?R))*)\)#(;if (preg_match_all($regex, $string ,$matches)) { echo implode(( (, $matches[1]);} else { //no parenthesis echo $string;}Or you can use preg_replace, but with multiple parenthesis you(ll lose the whitespace between them.$regex = (#[^()]*\((([^()]+|(?R))*)\)[^()]*#(;$replacement = (\1(;echo preg_replace($regex, $replacement, $string);I got a lot of help from this page, Finer points of PHP regular expressions.

posted date: 2009-04-09 20:26:00


Re: PHP: how to get only the words between a parenthesis () and clear everything else#8
Interesting, I didn't know that PHP supported recursive regular expressions.

posted date: 2009-04-09 20:45:00


Re: PHP: how to get only the words between a parenthesis () and clear everything else#9
Also assertions, backreferences and conditions. You can read it here: php.net/manual/en/…

posted date: 2009-04-09 21:11:00


Re: PHP: how to get only the words between a parenthesis () and clear everything else#10
function getInbetweenStrings($start, $end, $str){ $matches = array(); $regex = "/$start([a-zA-Z0-9_]*)$end/"; preg_match_all($regex, $str, $matches); return $matches[1];}for examle you want the array of strings(keys) between {} in following example, where (/( doesn(t fall in-between$str = "C://{ad_custom_attr1}/{upn}/{samaccountname}";$str_arr = getInbetweenStrings(({(, (}(, $str);print_r($str_arr);

posted date: 2013-02-26 07:55:00


Re: PHP: how to get only the words between a parenthesis () and clear everything else#11
Using above in a replaceecho preg_replace((/\(([\w]{1,2})\)/(,"(s\\1)",((Gs) Main Hall();results in(sGs) Main Hall

posted date: 2014-10-15 03:19:00


select page: « 1 »
Copyright ©2008-2017 www.momige.com, all rights reserved.