2016-07-29 6 views
3

文字列内の "%"内のすべての部分文字列を検索したいのですが、なぜ "id"しか見つからないのかわかりません。Regexが%PHP内のすべての部分文字列を検索する

$test = '<img src="%get_love%" alt="%f_id%" title="%id%" />'; 
$token_regex_inside_tags = "/<([^>]*%([\w]+)%[^>]*)>/"; 
preg_match_all($token_regex_inside_tags, $test, $matches); 

答えて

4

仮定: - 私はあなただけ<>間その中にあれば%内のコンテンツを見つける必要があると仮定しています。

あなたが正規表現で\G

(?:\G(?!\A)|<)[^%>]*%([^%>]*)% 

Regex Demo

正規表現の内訳

(?: 
    \G(?!\A) #End of previous match 
    | #Alternation 
    < #Match < literally 
) 
[^%>]* #Find anything that's not % or > 
%([^%>]*)% #Find the content within % 

を使用してこの正規表現を使用することができます

< #Matches < literally 
(
    [^>]* #Moves till > is found. Here its in end 
    %([\w]+)% #This part backtracks from last but is just able to find only the last content within two % 
    [^>]* 
)> 
+1

詳細な説明をありがとうございます! – coffeeak

関連する問題