Google Answers Logo
View Question
Q: PHP Regex Question ( Answered 5 out of 5 stars,   0 Comments )
Subject: PHP Regex Question
Category: Computers > Programming
Asked by: pakuaman-ga
List Price: $50.00
Posted: 15 Apr 2006 13:42 PDT
Expires: 15 May 2006 13:42 PDT
Question ID: 719256
I am looking for PHP code that will do the following:

Given the string:

The first meeting is <!--comment741-->Monday, April
7<!--comment000-->. Future meetings will be announced
<!--comment751-->here<!--comment000--> on this site.

Output an array:

[741] => Monday, April 7
[751] => here

In other words, the key is the numeric value that follows "comment" in
the first of each of the pairs of tags; and the value is the text
within the tags.

Notice that comment000 functions as a kind of closer for each tagged
bit of text and is not included in the array.

I am not highly experienced in PHP so any commenting of your code
would be appreciated.
Subject: Re: PHP Regex Question
Answered By: palitoy-ga on 17 Apr 2006 03:05 PDT
Rated:5 out of 5 stars
Hello pakuaman-ga,

Thank-you for your question.

Using regular expressions can be quite a daunting task but it is made
easier by breaking it down into several smaller chunks as you have
already begun to do in wording your question.

You have correctly identified that there are 4 pieces of information
and that we require to differentiate these from the rest of the text
using a regular expression.

Each of the <comments> you have indicated are followed by a 3-digit
number, in a regular expression this is notated as \d{3}.  To collect
a selection of text we would use the notation .*

Putting this together in your problem we get a reular expression that
looks like this:


The / characters indicate the beginning and end of the regular
expression.  Anything that is enclosed in () brackets indicate
something we are interested in.

Therefore in english this could be read something like this; to start
the pattern we need to find --comment that will be followed by 3
digits to be remembered as match number 1,  the 3 digits will be
immediately followed by --> and then some text that we will remember
as match 2.  We then expect to see the closing comment
<!--comment000--> followed by some more text before another numbered
comment, indicated by 3 digits that we will remember as match 3.  This
will again be closed by --> before some more text (that will be
remembered as match 4) before a closing <.

It sounds even more complicated written down like this!

Finally the code that will solve your problem is this:


# this is the text to be searched
$subject = "The first meeting is &lt;!--comment741--&gt;Monday, April
7&lt;!--comment000--&gt;. Future meetings will be announced
&lt;!--comment751--&gt;here&lt;!--comment000--&gt; on this site.";

# we now decode the &lt; and &gt; signs back to < and > for ease of use
$subject = html_entity_decode($subject);

# this is the regular expression pattern
$pattern = '/--comment(\d{3})-->(.*)<!--comment000-->.*<!--comment(\d{3})-->(.*)</';

# perform the regular expression search and store the results
# in an array called $matches
# $matches has the full string matched in [0], the first match
# in [1], the second match in [2], the third in [3] and so on
preg_match($pattern, $subject,$matches);

# begin an array to hold your information
$my_array = array();

# assign the information to your array
$my_array[$matches[1]] = $matches[2];
$my_array[$matches[3]] = $matches[4];

# output your array to prove it has worked!


The help files for PHP and the function required to perform this task
can be found here:

If you require any further assistance on this please do not hesitate
to ask for clarification and I will get back to you as soon as
pakuaman-ga rated this answer:5 out of 5 stars

There are no comments at this time.

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