Page 1 of 1

Preg Match All question

Posted: Sun Feb 12, 2006 1:51 pm
by Shendemiar

Code: Select all

<?
$source= "<a href=\"http://www.xxx.fi/index.php?Menu=Kunnat\" target=\"Paasivu\">\"Kunnat\" osiosta.</a>";

preg_match_all("/<a href(.*?)<\/a>/mi", $source, $matches);

echo var_export($matches,1);
?>
Outputs

Code: Select all

array (
  0 => 
  array (
    0 => '<a href="http://www.xxx.fi/index.php?Menu=Kunnat" target="Paasivu">"Kunnat" osiosta.</a>',
  ),
  1 => 
  array (
    0 => '="http://www.xxx.fi/index.php?Menu=Kunnat" target="Paasivu">"Kunnat" osiosta.',
  ),
)
How come it gives two matches?

Posted: Sun Feb 12, 2006 2:18 pm
by feyd
your usage of parenthesis.

Posted: Sun Feb 12, 2006 4:21 pm
by josh
To elaborate, the first match is the entire string that was matched, the second one is the extracted piece of the string that matched the part in parenthesis

Posted: Sun Feb 12, 2006 5:10 pm
by Shendemiar
jshpro2 wrote:To elaborate, the first match is the entire string that was matched, the second one is the extracted piece of the string that matched the part in parenthesis
First i was like WTF, but thats actually quite usefull...