Google Answers Logo
View Question
Q: PHP Regex ( Answered 5 out of 5 stars,   2 Comments )
Subject: PHP Regex
Category: Computers > Programming
Asked by: aaronfarr-ga
List Price: $5.00
Posted: 11 Aug 2006 20:06 PDT
Expires: 10 Sep 2006 20:06 PDT
Question ID: 755191
Hello all thanks for taking a look at my question.
I am in need of a regular expression that will parse out elements from a string.
The string format looks something like this: "[a][b][c][something else][etc..]".
I would like to be able to parse out everything within the brackets
(a,b,c,something else, etc..).  I believe regex is capable of doing
such a task but I am not that familiar with it so I am turning to my
fellow googlers for some help.  Thanks.
Subject: Re: PHP Regex
Answered By: sycophant-ga on 12 Aug 2006 22:55 PDT
Rated:5 out of 5 stars

The function you are looking for is preg_match_all()
See this page for details:
Also, the general PCRE page:

Here is a little example that deals with the string formatting you mentioned:
$string = "[a][bc][def][ghij][klmno]";

$regex = "/\[([^\]]+)/";


foreach ($res[1] as $match) {
	echo "Found: $match\n";

$commastring = join(",",$res[1]);

echo "Result: $commastring";


$res is an array of the elements matched between the ()'s in the
regular expression. The first element (0) is the whole input string.
The second element (1) will contain the content of the first set of
brackets as an array of matches from within the input. So by looping
through $res[1] with 'foreach' we can deal with each match from the

Or, to recombine then as a comma-delimited string we use 'join' to put
all the elements of the array together with a comma between them.

I hope this helps. 

Let me know if it is still unclear.
aaronfarr-ga rated this answer:5 out of 5 stars
Excellent answer!

Subject: Re: PHP Regex
From: amazeworld-ga on 12 Aug 2006 18:02 PDT
You want to use this function?

you can try it:[d][e][fdads][fef][r5353][r3ad]
or[a][b][c][something else][etc..]
Subject: Re: PHP Regex
From: aaronfarr-ga on 13 Aug 2006 02:10 PDT
Thank you amazeworld.  The links you provided certainly do the trick
but the accepted answer provided the source code which was needed. 
Thanks again for your time I do appreciate it.  Best Regards.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  

Google Home - Answers FAQ - Terms of Service - Privacy Policy