这里的问题:
我有两个div#发布播放列表列表和发布播放列表描述
我想使用两个短代码[文本文章]和[播放列表文章]
我需要一个函数,它可以获取[texte article][/texte article]之间的所有内容,并将其放入#post播放列表列表
以及将[播放列表文章][播放列表文章]内容获取到#后播放列表描述中的另一功能
我在preg_replace开始学习,但语法并不是那么容易学。
我你可以帮我做这件事,那就太好了。
好的,我试过这个函数,我没有错误,但它似乎没有返回任何东西难道我不需要逃离Regexp吗?如"/[texte article](.*)[/texte article]/"
这是我的单曲中的代码。hp:
<div id="post-playlist-list" class="box">
<?php
$id = get_the_ID();
$post = get_post($id);
$content = apply_filters('the_content', $post->post_content);
$content = str_replace( ']]>', ']]>', $content );
echo $content;
?>
</div>
<div id="post-playlist-description" >
<?php
echo $content;
?>
</div>
这是我的单曲中的代码。hp:
function texte_article_function() {
preg_replace_callback("/'[texte-article'](.*)'['/texte-article']/", function($matches) {
//$matches contains an array of all matches
//$matches[0] stores the full pattern match: '[texte-article](.*)'['/texte-article]
//$matches[1] stores the first subpattern match: (.*)
//Do whatever text parsing you need to do here and return the result.
$content = $matches[0];
return $content;
}, $postdata);
}
function playlist_article_function() {
preg_replace_callback("/'[playlist-article'](.*)'['/playlist-article']/", function($matches) {
//$matches contains an array of all matches
//$matches[0] stores the full pattern match: '[texte-article](.*)'['/texte-article]
//$matches[1] stores the first subpattern match: (.*)
//Do whatever text parsing you need to do here and return the result.
$content = $matches[0];
return $content;
}, $postdata);
}
function register_shortcodes(){
add_shortcode('texte-article', 'texte_article_function');
add_shortcode('playlist-article', 'playlist_article_function');
}
add_action( 'init', 'register_shortcodes');
感谢所有的答案:)以下是使用regex:的解决方案
functions.php
function texte_article_function($content_texte_article) {
//Get [texte-article] content
$content_texte_article = preg_replace('#.*'[texte-article'](.*)'['/texte-article'].*#s', '$1', $content_texte_article);
return $content_texte_article;
}
function playlist_article_function($content_playlist_article) {
//get [fap_playlist] content
$content_playlist_article = preg_replace('#.*'[playlist-article'](.*)'['/playlist-article'].*#s', '$1', $content_playlist_article);
return $content_playlist_article;
}
回调函数:
<?php
$id = get_the_ID();
$post = get_post($id);
$content_playlist_article = $content;
$btn_play_index_id = $content;
echo playlist_article_function($content_playlist_article);
// echo $content;
?>
<?php
$content_texte_article = $content;
echo texte_article_function($content_texte_article);
?>
如果它能帮助别人。