-1

Here is my code:

preg_match_all('/<a href="(.+?)index.php(.+?)&abc=(.+?)"/', $dataToParse, $matches);

foreach ($matches as $val)
{
    $absUrl = $val[1] . 'index.php' . $val[2] . '&abc=' . $val[3];

    echo $absUrl;
}

However, $val[1] is the entire matched string, including the <a href. I believe I have the syntax wrong but I have been trying to fix it with no luck. Not sure how to do this properly.

Andy Lester
  • 86,927
  • 13
  • 98
  • 148
John Smith
  • 8,688
  • 13
  • 49
  • 66

2 Answers2

4

Try passing the constant PREG_SET_ORDER after the $matches one, like so:

preg_match_all("/.../",$dataToParse,$matches,PREG_SET_ORDER);

For more information as to why, see the documentation

Niet the Dark Absol
  • 311,322
  • 76
  • 447
  • 566
0

$matches[0] is the whole string, $matches[1] is the first match group, $matches[2] the second match group and so on.

for( $i = 0; $i < count( $matches[1]); $i++)
{
    $absUrl = $matches[1][$i] . 'index.php' . $matches[2][$i] . '&abc=' . $matches[3][$i];
    echo $absUrl;
}
nickb
  • 58,150
  • 12
  • 100
  • 138
circusdei
  • 1,927
  • 12
  • 27