2015-03-03 72 views
1

我需要獲取wordpress短代碼之間的內容。在短代碼之間獲取內容

好比說[bla_blabla name="a"]Variation A[/bla_blabla]

應該返回Variation A

我想使用正則表達式來得到它的,但有一個shortcodes.php文件在WordPress應該已經能夠這樣做呢?你能指導我採取正確的做法嗎?

我已經目前使用此代碼

<?php 
function abtest_runner($input) { 
    //A bit confused if you wanted equal chances of selecting one or equal changes? 
    //This is for equal chances. 
    $size = count($input); 
    $select = rand(0,$size-1); 
    $temp=array(); 
    $selection = $input[$select]; 
    $temp = (explode("]",$selection)); 
    $temp = (explode("[",$temp[1])); 
    echo $temp[0]; 

} 

$input[0] = '[bla_blabla name="a"]Variation A[/bla_blabla]'; 
$input[1] = '[bla_blabla name="b"]Variation B [/bla_blabla]'; 

abtest_runner($input); 
?> 
+0

你正在努力達到什麼目的?可能有一個更簡單的方法! – 2015-03-03 07:08:18

回答

2

如果你已經註冊你的WordPress使用[add_shortcode][1]功能,那麼,你可以使用下面的方法提取的簡碼的內容簡碼:

$pattern = get_shortcode_regex(); 
$content = '[bla_blabla name="a"]Variation A[/bla_blabla]'; 
$matches = array(); 
preg_match("/$pattern/s", $content, $matches); 
print_r($matches[5]); 

$matches[5]將包含您的內容。

希望這會有所幫助。