php-提取内容中的简码参数-WordPress
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php-提取内容中的简码参数-WordPress,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1756字,纯文字阅读大概需要3分钟。
内容图文
![php-提取内容中的简码参数-WordPress](/upload/InfoBanner/zyjiaocheng/682/9e9677f4cf464f6a8331fbb7cfc882d1.jpg)
考虑如下的帖子内容:
[shortcode a="a_param"]
... Some content and shortcodes here
[shortcode b="b_param"]
.. Again some content here
[shortcode c="c_param"]
我有一个接受3个或更多参数的简码.
我想找出在内容及其数组中的参数中使用简码的次数,例如
array (
[0] => array(a => a_param, b=> null, c=>null),
[1] => array(a => null, b=> b_param, c=>null),
[2] => array(a => null, b=> null, c=>c_param),
)
我需要在the_content过滤器,wp_head过滤器或类似的工具中执行此操作.
我怎样才能做到这一点 ?
谢谢,
解决方法:
在wordpress get_shortcode_regex()中,函数返回用于在帖子内部搜索短代码的正则表达式.
$pattern = get_shortcode_regex();
然后将模式与发布内容进行preg_match
if ( preg_match_all( '/'. $pattern .'/s', $post->post_content, $matches ) )
如果返回true,则提取的短代码详细信息将保存在$matches变量中.
尝试
global $post;
$result = array();
//get shortcode regex pattern wordpress function
$pattern = get_shortcode_regex();
if ( preg_match_all( '/'. $pattern .'/s', $post->post_content, $matches ) )
{
$keys = array();
$result = array();
foreach( $matches[0] as $key => $value) {
// $matches[3] return the shortcode attribute as string
// replace space with '&' for parse_str() function
$get = str_replace(" ", "&" , $matches[3][$key] );
parse_str($get, $output);
//get all shortcode attribute keys
$keys = array_unique( array_merge( $keys, array_keys($output)) );
$result[] = $output;
}
//var_dump($result);
if( $keys && $result ) {
// Loop the result array and add the missing shortcode attribute key
foreach ($result as $key => $value) {
// Loop the shortcode attribute key
foreach ($keys as $attr_key) {
$result[$key][$attr_key] = isset( $result[$key][$attr_key] ) ? $result[$key][$attr_key] : NULL;
}
//sort the array key
ksort( $result[$key]);
}
}
//display the result
print_r($result);
}
内容总结
以上是互联网集市为您收集整理的php-提取内容中的简码参数-WordPress全部内容,希望文章能够帮你解决php-提取内容中的简码参数-WordPress所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。