I have a CMS that uses a syntax based on HTML comments to let the user insert flash video players, slideshows, and other 'hard' code that the user could not easily write.
The syntax for one FLV movies looks like this:
<!--PLAYER=filename.flv-->
I use this code:
$find_players = preg_match("/<!--PLAYER\=(.*)-->/si", $html_content, $match);
This works great if there is only one player, $match[1] contains the filename (which is all I need)
My knowledge of regex is vanishing, so I'm not able to adjust this to grab more than one match.
If there are more on the page, it breaks totally, because it matches too greedily (from the first <!--PLAYER
to the last -->
You probably want the regex modifier U (PCRE_UNGREEDY, to match ungreedily). This will fetch the shortest possible match, meaning that you won't match from the beginning of the first <!--PLAYER= to the end of the last -->
An abbreviated example:
<?php
$text = "blah\n<!-x=abc->blah<!-x=def->blah\n\nblah<!-x=ghi->\nblahblah" ;
$reg = "/<!-x=(.*)->/U" ;
preg_match_all( $reg, $text, $matches ) ;
print_r( $matches ) ;
Your code then becomes:
$find_players = preg_match_all("/<!--PLAYER=(.*)-->/Ui", $html_content, $matches);
// print $matches[1] ;
The 's' modifier (PCRE_DOTALL) you're using probably isn't helpful, either; you're unlikely to have a filename with a linebreak in it.
EDIT: @Stevens suggests this syntax, which I agree is slightly clearer - moving the U modifier to the capturing parentheses.
$find_players = preg_match_all("/<!--PLAYER=(?U)(.*)-->/i", $html_content, $matches);