【发布时间】:2023-03-13 01:30:01
【问题描述】:
如何使此过滤器适用于同一文本中的多个匹配项?
function _embed_link($text, $filter, $format) {
if (preg_match_all("/\[(.*?)\|(node|term)\:(\d+)\]/i", $text, $params)) {
foreach ($params[0] as $key => $param) {
$args = array(
$params[0][$key],
$params[1][$key],
$params[2][$key],
$params[3][$key],
);
$markup = _embed_link_process($args);
$text = preg_replace("/\[(.*?)\|(node|term)\:(\d+)\]/", $markup, $text, 1);
}
}
return $text;
}
这是返回链接的函数
function _embed_link_process($params = array()) {
$output = '';
if ($params[2] == 'node') {
// Find node by it's id
$node = node_load($params[3]);
$output .= render(l($params[1], 'node/'. $node->nid, array(
'attributes' => array(
'class' => array('embed-link', 'embed-link-node', 'embed-link-node-'. $node->nid),
),
)));
}
if ($params[2] == 'term') {
$term = taxonomy_term_load($params[3]);;
$output .= render(l($params[1], 'taxonomy/term/'. $term->tid, array(
'attributes' => array(
'class' => array('embed-link', 'embed-link-term', 'embed-link-term-'. $term->tid),
),
)));
}
return $output;
}
使用过滤器的示例文本:
Ut [点击这里|node:4] enim ad minim veniam, quis [点击这里|term:42] nostrud 练习 ullamco 劳里斯 nisi ut aliquip ex ea commodo 结果。 Duis aute irure dolor in reprehenderit in voluptate velit esse cillum [点击这里|term:1] dolore eu fugiat nulla pariatur。 Exceptioneur sint occaecat cupidatat non proident, sunt in culpa [点击 here|node:42] qui officia deserunt mollit anim id est labourum。
目标是将每个 [...] 替换为正确的链接。
【问题讨论】: