On preg match php


On preg match php

我需要帮助创建正则表达式

blah blah blah blah <?= __l(array('key'=>'SOMEVALUE','default'=>'string string string')) ?>blah blah

我希望能够剥离"SOEVALUE"answers"字符串字符串"

谢谢提前

======"这就是我所拥有的"===

$subject = "Blah blah ja ja blah blah jank junk jonk <?= __l(array('key'=>'KEYKEYKEY','default'=>'I am a monkey sigh''s')) ?> ldjlsakfdj as;dfj as;flkj a fsd  ljaasfd <?= __l(array('key'=>'KEYKEYKEY','default'=>'I am a monkey sigh''s')) ?>  ";
$pattern = '#'_'_l'(array'(''key''=>''(.*)'',''default''=>''(.*)''')')#';

if (preg_match_all($pattern, $subject)) {
print "A match was found.";
} else {
print "A match was not found.";
}
print '<br />';
preg_match_all($pattern, $subject, $matches);
echo '<pre>';
print_r($matches);
echo '</pre>'

您的正则表达式有点太复杂了,请尝试以下操作:

$regex = "/'key'=>'[^']+','default'=>'[^']+'/";
$string = preg_replace( $regex, "'key'=>'','default'=>''", $string);

正则表达式为:

'key'=>'        - Match this literally
[^']+           - Match anything that's not a single quote, one or more times
','default'=>'  - Match this literally
[^']+           - Match anything that's not a single quote, one or more times
'               - Match this literally

而替换只是CCD_ 1的第二个参数。

这是我得出的答案:

$pattern = '#'_'_l'(array'(''key''=>''(.*?)'',''default''=>''(.*?)''')')#';

但我喜欢你更好地回答nickb,所以我会投票支持你的