2012-07-30 56 views
1

我需要帮助创建一个正则表达式在预浸比赛PHP

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

我希望能够剥离“someValue中”和“串串串”

感谢 在提前

======'这是我有'========

$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

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

$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()

1

这里是我想出了答案:

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

,但我喜欢你的答案更好nickb,所以我会投你了