preg的小问题全部匹配

what's the problem with the follow? It does not echo something..

$string = '<embed src="http://www.youtube.com/v/XBH1dcHoL6Y&rel=0&hl=en_US&feature=player_embedded&­version=3" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="640" height="390"></embed>'; 

preg_match('#<embed[^>]+>.+?http://www.youtube.com/v/([A-Za-z0-9\-_]+).+?</embed>#s', $string, $matches); 
echo $matches[1];

Because of this: <embed[^>]+>.+?http. That means:

  1. match the string <embed
  2. followed by one or more characters that is not >
  3. followed by a >
  4. followed by any number of characters (non-greedily)
  5. followed by http

Your string does not match those criteria because you don't have a > before your http. If you remove the [^>]+> bit your regular expression should match.

Now you know one of the problems with parsing HTML with a regular expression. You shouldn't. You should use a DOM parser instead.

it is actually possible to parse html with regexps. have a look at for example smarty-s source to see how it parses tags :P but it is not the way to do it.

try an xmlparser or domdocument

preg_match('#([\w\-]+){11}?#is', $string, $matches);