次のような文字列があります。
$foo = 'Hello __("How are you") I am __("very good thank you")'
私はそれが奇妙な文字列であることを知っていますが、私と一緒にいてください:P
__("Look for content here") の間のコンテンツを探して配列に入れる正規表現が必要です。
つまり、正規表現は、"How are you" と "very good thank you" を検索します。
次のような文字列があります。
$foo = 'Hello __("How are you") I am __("very good thank you")'
私はそれが奇妙な文字列であることを知っていますが、私と一緒にいてください:P
__("Look for content here") の間のコンテンツを探して配列に入れる正規表現が必要です。
つまり、正規表現は、"How are you" と "very good thank you" を検索します。
これを試して:
preg_match_all('/(?<=__\(").*?(?="\))/s', $foo, $matches);
print_r($matches);
つまり:
(?<= # start positive look behind
__\(" # match the characters '__("'
) # end positive look behind
.*? # match any character and repeat it zero or more times, reluctantly
(?= # start positive look ahead
"\) # match the characters '")'
) # end positive look ahead
編集
Greg が述べたように、ルックアラウンドにあまり慣れていない人は、ルックアラウンドを省略した方が読みやすいかもしれません。次に、すべてを一致させます: __("
、文字列、および文字列,に")
一致する正規表現を括弧で囲み、それらの文字のみをキャプチャします。ただし、一致するものを取得する必要があります。デモ:.*?
$matches[1]
preg_match_all('/__\("(.*?)"\)/', $foo, $matches);
print_r($matches[1]);
Gumbo の提案を使用したい場合は、次のパターンで彼の功績が認められます。
$foo = 'Hello __("How are you")I am __("very good thank you")';
preg_match_all('/__\("([^"]*)"\)/', $foo, $matches);
$matches[1]
完全な文字列の結果が必要でない限り、必ず結果に使用してください。
var_dump()
の$matches
:
array
0 =>
array
0 => string '__("How are you")' (length=16)
1 => string '__("very good thank you")' (length=25)
1 =>
array
0 => string 'How are you' (length=10)
1 => string 'very good thank you' (length=19)