我需要帮助创建正则表达式
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>'
答案 0 :(得分: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 :(得分:1)
以下是我提出的答案:
$pattern = '#\_\_l\(array\(\'key\'=>\'(.*?)\',\'default\'=>\'(.*?)\'\)\)#';
但我希望你能更好地回答 nickb ,所以我会投票给你