Link to home
Start Free TrialLog in
Avatar of skij
skijFlag for Canada

asked on

PHP/REGEX: Put matches inside array

Using PHP and REGEX, how can I put the content between the "<!--xContent-->" tags inside of an array?

This:
<!--xContent-->
Hello World
<!--/xContent-->
xyz
<!--  xContent -->
Example
<!--/xContent -->
Blah blah blah
<!-- xContent -->
Hi
<!-- /xContent -->

Open in new window

Should become this:
Array
(
    [0] => Hello World
    [1] => Example
    [2] => Hi
)

Open in new window

Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

<?php

$mystring = '<!--xContent-->
Hello World
<!--/xContent-->
xyz
<!--  xContent -->
Example
<!--/xContent -->
Blah blah blah
<!-- xContent -->
Hi
<!-- /xContent -->';

preg_match_all('#<\!--\s*xContent\s*-->\s*(.*?)\s*<!--\s*/xContent\s*-->#s', $mystring, $matches);
print_r($matches[1]);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why not simplify your life and convert it to XML formatted file and use php XML related tools to read/write the file as when needed.