2012-07-30 69 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,所以我會投你了