1

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

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

我希望能够剥离 'SOMEVALUE' 和 'string string string'

提前致谢

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

$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>'
4

2 回答 2

4

你的正则表达式有点太复杂了,试试这样的:

$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

而替换只是preg_replace().

于 2012-07-30T01:43:16.987 回答
1

这是我想出的答案:

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

但我喜欢你回答更好的nickb所以我会投票给你

于 2012-07-30T01:56:28.177 回答