1

I am trying to extract substrings matching given regex expression from the string below:

"Lorem ipsum dolor sit amet. <xy:abc_ref d_id="1234">Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.<xy:abc_ref d_id="1234">"

Regex does match it as expected. However, for some reason I can only access the first parsed value. Even though the counter (count($matches)) states there are two results, see the output.

$value = 'Lorem ipsum dolor sit amet. <xy:abc_ref d_id="1234">Lorem ipsum dolor sit amet. <xy:abc_ref d_id="5678">Lorem ipsum dolor sit amet.<xy:abc_ref d_id="1234">';

The source:

function test($value)
{
    $RegEx = '/<xy:abc_ref ([^>]{0,})>/';       
    $n = preg_match($RegEx,$value,$matches);
    print("Results count: " . count($matches)."<br>");
    print("matches[0]: " . $matches[0]."<br>");
    print("matches[1]: " . $matches[1]."<br>");
    print("matches[2]: " . $matches[2]."<br>");
}
echo test($value);

The output:

Results count: 2
matches[0]:
matches[1]: d_id="1234"
matches[2]: 

Thanks, LG

Luke G
  • 1,741
  • 6
  • 23
  • 34
  • 1
    Sounds like a job for an XML parser, rather than a regex solution. [How to parse and process HTML/XML?](http://stackoverflow.com/q/3577641) – Madara's Ghost Apr 13 '13 at 13:47

3 Answers3

2

Use preg_match_all to get all matches. preg_match will only return the first match. Count will return two because you capture a group.

alexn
  • 57,867
  • 14
  • 111
  • 145
2

You must use preg_match_all instead.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
0
function test($value)
{
    $RegEx = '/<xy:abc_ref ([^>]{0,})>/';       
    $n = preg_match_all($RegEx,$value,$matches);
    print("Results count: " . count($matches[1])."<br>");
    print("matches[0]: " . $matches[1][0]."<br>");
    print("matches[1]: " . $matches[1][1]."<br>");
    print("matches[2]: " . $matches[1][2]."<br>");
}
ChaseTheSun
  • 3,810
  • 3
  • 18
  • 16