Preg_Replace和Capture Value? PHP
I'm building a filter plugin in Wordpress and I'm replacing some plugin specific tags with bits of html.
Example: [VIDEO ID=12] will be replaced via preg_replaced in this function
function display_video($text){
$pattern = '/\[VIDEO ID\=\d+\]/';
$text=preg_replace($pattern,get_video_block($id),$text);
return $text;
}
I'm not exactly sure how to make sure I supply the correct ($id) param to my get_video_block function for each replacement occurrence.
There's no real loop going on other than inside the preg_replace function so, how would I supply that value?
Thoughts?
EDIT (get_video_block) function:
function get_video_block($id){
global $wpdb;
$wpdb->show_errors();
$table_name = $wpdb->prefix . "video_manager";
$query = "SELECT * FROM " . $table_name . " WHERE `index` = '$id'";
$results = $wpdb->get_results($query, ARRAY_A);
$results = $results[0];
$returnString = '<div class="vidBlock">';
$returnString .= $results['embed_code'];
$returnString .= '<div class="voteBar">';
$returnString .= $results['vote_text'];
$returnString .= '<input type="button" value="YES" class="voteButton">';
$returnString .= '<input type="button" value="NO" class="voteButton">';
$returnString .= '</div>';
$returnString .= $results['title'] . '<br>';
$returnString .= $results['description'] . '<br>';
$returnString .= '</div>';
return $returnString;
}
我正在Wordpress中构建一个过滤器插件,我正在用一些html替换一些插件特定的标签。 / p>
示例:[VIDEO ID = 12]将通过此函数中的preg_replaced替换 p>
function display_video($ text){
\ n $ pattern ='/ \ [VIDEO ID \ = \ d + \] /';
$ text = preg_replace($ pattern,get_video_block($ id),$ text);
返回$ text; \ n}
code> pre>
我不确定如何确保为每次替换事件向get_video_block函数提供正确的($ id)参数。 p >
除了在preg_replace函数内部之外没有真正的循环,所以,我将如何提供该值? p>
思考? p>
EDIT(get_video_block)函数: p>
function get_video_block($ id){
global $ wpdb;
$ wpdb-&gt; show_errors();
$ table_name = $ wpdb-&gt;前缀。 “video_manager”;
$ query =“SELECT * FROM”。 $ table_name。 “WHERE`index` ='$ id'”;
$ results = $ wpdb-&gt; get_results($ query,ARRAY_A);
$ results = $ results [0];
$ returnString ='&lt; div class =“vidBlock”&gt;' ;
$ returnString。= $ results ['embed_code'];
$ returnString。='&lt; div class =“voteBar”&gt;';
$ returnString。= $ results ['vote_text'];
$ returnString。='&lt; input type =“button”value =“YES”class =“voteButton”&gt;';
$ returnString。='&lt; input type =“button”value =“NO”class =“voteButton”&gt;';
$ returnString。='&lt; / div&gt;';
$ returnString。= $ results ['title']。 '&lt; br&gt;';
$ returnString。= $ results ['description']。 '&lt; br&gt;';
$ returnString。='&lt; / div&gt;';
返回$ returnString;
}
code> pre>
DIV>
You can use preg_replace_callback()
for that purpose. You'll also need to wrap \d+
in (
parentheses )
so it can be captured and used in the callback function.
function display_video($text) {
$callback = create_function('$matches', 'return get_video_block($matches[1])');
return preg_replace_callback('/\[VIDEO ID\=(\d+)\]/', $callback, $text);
}
Note that $matches[1]
is used because $matches[0]
contains the entire string matched by the regular expression.
Erwin's comment may be of use to you — WordPress has a shortcode API that manages parsing of shortcodes for you, so you can concentrate on dealing with what you want to do with the shortcode attributes.