Google Answers Logo
View Question
 
Q: PHP Regex ( Answered 5 out of 5 stars,   2 Comments )
Question  
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.
Answer  
Subject: Re: PHP Regex
Answered By: sycophant-ga on 12 Aug 2006 22:55 PDT
Rated:5 out of 5 stars
 
Hi, 

The function you are looking for is preg_match_all()
See this page for details:
http://php.net/manual/en/function.preg-match-all.php
Also, the general PCRE page:
http://php.net/manual/en/ref.pcre.php

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

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

preg_match_all($regex,$string,$res);

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
input.

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!

Comments  
Subject: Re: PHP Regex
From: amazeworld-ga on 12 Aug 2006 18:02 PDT
 
You want to use this function?
http://cn2.php.net/preg_match

you can try it:
http://www.naturer.net/answers/as.php?userstr=[d][e][fdads][fef][r5353][r3ad]
or
http://www.naturer.net/answers/as.php?userstr=[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 answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


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