嗨,我如何在上进行预赛
$string1 = "[%refund%]processed_by"
$string2 = "[%refund%]date_sent"
我想获取%%内部的位,然后完全删除[%item%]
。只留下"processed_by"或"date_sent",我在下面尝试了一下,但有点卡住了。
$unprocessedString = "[%refund%]date_sent"
$match = preg_match('/^'[.+']/', $unprocessedString);
$string = preg_replace('/^'[.+']/', $unprocessedString);
echo $match; // this should output refund
echo $string; // this should output date_sent
您的问题在于使用preg_match
函数。它返回找到的匹配项的个数。但是,如果将一个变量作为第三个参数传递给它,它会将整个模式及其子模式的匹配项存储在一个数组中。
因此,您可以使用preg_match
在子模式中捕获您想要的两个部分,这意味着您不需要preg_replace
:
$unprocessedString = "[%refund%]date_sent"
preg_match('/^'[%(.+)%'](.+)/', $unprocessedString, $matches);
echo $matches[1]; // outputs 'refund'
echo $matches[2]; // outputs 'date_sent'