-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
  • 91,102
  • 13
  • 100
  • 152
John Smith
  • 8,567
  • 13
  • 51
  • 74

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
  • 320,036
  • 81
  • 464
  • 592
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
  • 59,313
  • 13
  • 108
  • 143
circusdei
  • 1,967
  • 12
  • 28