2012-09-23 65 views
0

如何使此过滤器在同一文本中的多个匹配上工作?文本过滤器,多个匹配

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 [按此|节点:4] enim广告微量veniam,QUIS [点击这里|词条:42] nultrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat。 Duis aute irure dolor in rennederit in voluptate velit esse cillum [点击这里|词条:1] dolore eu fugiat nulla pariatur。 Excepteur sint occaecat cupidatat non proident,sunt in culpa [点击这里查看 |节点:42] qui officia deserunt mollit anim id est laborum。

目标是让每个代替正确的链接。

回答

0

修改功能_embed_link使用preg_match_all

function _embed_link($text, $filter, $format) { 
    if (preg_match_all("/\[(.*?)\|(node|term)\:(\d+)\]/i", $text, $params)) { 
    foreach ($params[0] as $key => $param) { 
     // $param => [Click here|node:4] 
     $param_arr = array (
     $params[0][$key], // [Click here|node:4] 
     $params[1][$key], // Click here 
     $params[2][$key], // node 
     $params[3][$key], // 4 
    ); 
     $markup = _embed_link_process($param_arr); 
     $text = str_replace($param, $markup, $text); 
    } 
    } 

    return $text; 
} 
+0

的作品就像一个魅力,谢谢 –

0

查看documentation of preg_replace

最后一个参数(1)明确表示您只想替换第一个匹配项。删除它,所有的事件应该被替换。