Page 1 of 1

How To Remove Anything Outside Square Brackets

Posted: Tue May 01, 2007 9:18 pm
by shehabic
hello

I Need A Sysntax On How To Remove what's Outside Square Brackets :
e.g if my String is

Code: Select all

$X='blablablablablabla[TEST_1]blablablablabla[TEST_2_TEST]blablab123445!$%$#^&^&$*_#$()SGJBLABLAlabla[TEST_3_ANYTHING_3]';

I Want It to Be : 

' [TEST_1] [TEST_2_TEST] [TEST_3_ANYTHING_3] '
The Following Code Is Exactly The Inverse Of What I Want :

Code: Select all

preg_replace('/\[\w[^\]]*\]/s',' ',$X);

Posted: Tue May 01, 2007 9:29 pm
by feyd
Why not use preg_match_all() to find all the parts in brackets?

Posted: Tue May 01, 2007 9:37 pm
by shehabic
Thanks A Lot ,
How Comes I Didn't Think About This . .

I Used This And It Worked

Code: Select all

preg_match_all('/\[\w[^\]]*\]/s',$X,$FINAL);
echo implode(",",$FINAL[0]);
Anyway I Still Wanna Know It Just For Knowledge (If Anyone Knows How To Do What I Asked For) In The 1st Post

Posted: Wed May 02, 2007 3:17 am
by stereofrog

Code: Select all

preg_replace('/(^|\]).*?($|\[)/', '', $X);
If possible, please use proper spelling.