1
我知道preg_replace_callback對於這個目的是理想的,但我不知道如何完成我開始的工作。使用preg_replace_callback DIY PHP短代碼?
你可以看到我想要實現 - 我只是不知道如何使用回調函數:
//my string
$string = 'Dear [attendee], we are looking forward to seeing you on [day]. Regards, [staff name]. ';
//search pattern
$pattern = '~\[(.*?)\]~';
//the function call
$result = preg_replace_callback($pattern, 'callback', $string);
//the callback function
function callback ($matches) {
echo "<pre>";
print_r($matches);
echo "</pre>";
//pseudocode
if shortcode = "attendee" then "Warren"
if shortcode = "day" then "Monday"
if shortcode = "staff name" then "John"
return ????;
}
echo $result;
所需的輸出將是Dear Warren, we are looking forward to seeing you on Monday. Regards, John.