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:
/--comment(\d{3})-->(.*)<!--comment000-->.*<!--comment(\d{3})-->(.*)</
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:
<?php
# this is the text to be searched
$subject = "The first meeting is <!--comment741-->Monday, April
7<!--comment000-->. Future meetings will be announced
<!--comment751-->here<!--comment000--> on this site.";
# we now decode the < and > 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!
print_r($my_array);
?>
The help files for PHP and the function required to perform this task
can be found here:
http://www.php.net/manual/en/function.preg-match.php
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
possible. |