【发布时间】:2017-05-06 10:45:45
【问题描述】:
我知道 preg_replace_callback 非常适合此目的,但我不确定如何完成我已经开始的工作。
您可以看到我想要实现的目标 - 我只是不确定如何使用回调函数:
//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.
【问题讨论】:
标签: php regex preg-replace-callback