2014-09-28 43 views
0

我想在body標籤後面的內容上使用shortcode函數。如何使用preg_replace反向引用而不是preg_match和preg_replace?

preg_match("/<body[^>]*>(.*?)<\/body>/is", $html, $matches); 
$html= preg_replace("/<body[^>]*>(.*?)<\/body>/is",run_shortcodes($matches[1]), $body); 
echo $html; 

這工作正常,但我想這樣做preg_replace反向引用,如果可能的話。

我已經試過這樣的事情,但顯然這是錯誤的

$html= preg_replace("/<body[^>]*>(.*?)<\/body>/is",run_shortcodes('$1'), $body); 

可有人請出示使用反向引用的一個可能的例子。

任何幫助表示讚賞。

回答

1

這裏有一個例子:

preg_match("/<body[^>]*>(.*?)<\/body>/is", $html, $matches); 
$newBody = run_shortcodes($matches[1]); 
echo preg_replace('/(.*<body[^>]*>)(.*)(<\/body>.*)/is', '${1}'.$newBody.'${3}', $html); 

你還可以嘗試:

$html = preg_replace_callback("/<body[^>]*>(.*?)<\/body>/is", 'run_shortcodes', $subject); 
echo $html;