Preg Match All question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Preg Match All question

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your usage of parenthesis.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post 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...
Post Reply