dongyun65343 2012-07-31 10:22
浏览 59

用PHP代码或函数替换短代码字符串?

im trying to develop a shortcode replacer but can't really get the hang of it. What i want to do is that the user can write something like [slideshow]10[/slideshow] and then make so slideshow with id number 10 appears. This is as far as i've come:

$slideArray = array(preg_match_all('/\[slideshow\](.*?)\[\/slideshow\]/is', $post, $matches));

With this i then:

for ($o = 0; $o <= $count; $o++) 
   { 
   $query = mysql_query("SELECT * FROM slideshow WHERE id = ''. $matches[1][$o]. '' LIMIT 1");
   while ($row = mysql_fetch_assoc($query))
   {
    // Slideshow code appears here
   } 
}

This works but now i have to decide if the slideshow appears in the top of the post or the bottom. But if i want to make so this slideshow appears where the user has written it. So it would be possible that it appears for an example in the middle of the text. Then is it possible to create function of this and then use preg_replace to replace [slideshow]10[/slideshow] with the query code above. I'm sorry for my poor english.

Best Regards

  • 写回答

1条回答 默认 最新

  • douba8758 2012-07-31 12:45
    关注

    Add something like this in the "// Slideshow code appears here" area

    $post = str_replace($matches[0][$o], $row['slideshowfield'], $post);
    

    All code should look something like:

    preg_match_all('/\[slideshow\](.*?)\[\/slideshow\]/Usi', $post, $matches);
    
    if(!empty($matches[1])){
        foreach($matches[1] as $o => $slideshow_id){
            $query = mysql_query("SELECT * FROM slideshow WHERE id = '". (int)$slideshow_id. "' LIMIT 1");
            if($row = mysql_fetch_array($query)){
                $post = str_replace($matches[0][$o], $row['slideshow_html_fieldname'], $post);
            }
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测