-1

我应该说我不是英语,所以对单词问题感到抱歉!我想在我的模板系统中显示帖子标签,这个工作很完美:

     <?php
$text= "hi,ok,bye";
    $numKeys=substr_count($text, ',');
    for($i=0;$i<=$numKeys;$i++){
        $exp=explode(",",$text);
        $tags=$exp[$i];
        echo "<a href='http://test.com/$tags'><strong>$tags</strong></a> ";
    }
?> 

但是当我在我的模板系统中使用它时它不起作用(发布标题,时间,......工作完美只是发布标签不起作用!(只显示第一个标签!只有一个标签,如:嗨)):

     //posts
$start = strpos($theme, '<start>');
$end = strpos($theme, '</end>', $start + strlen('</end>'));
$posty = substr($theme, $start, $end - $start);
$post = $posty;
$post = str_replace('<start>','',$post);
$post = str_replace('</end>','',$post);
$post_query = mysql_query("SELECT * FROM `posts` WHERE `bid`='{$bid}' ORDER BY `id` DESC");
$posts = '';
if($post_query) {
    while($post_data = mysql_fetch_array($post_query)) {
        $postid = $post_data['id'];
        $post_temp = $post;
        $post_temp = str_replace('[post_title]', $post_data['title'], $post_temp);
        $post_temp = str_replace('[post_content]', $post_data['content'], $post_temp);
        $post_temp = str_replace('[post_date]',$post_data['date'], $post_temp);
        $post_temp = str_replace('[post_author]',$post_data['author'],$post_temp);
        $post_temp = str_replace('[post_comments]',$post_data['comments'],$post_temp);
        $post_temp = str_replace('[post_full_content]','',$post_temp);


$text= "hi,ok,bye";
    $numKeys=substr_count($text, ',');
    for($i=0;$i<=$numKeys;$i++){
        $exp=explode(",",$text);
        $tags=$exp[$i];
        $post_temp = str_replace('[post_tag]',$tags,$post_temp);
    }


        $posts .= $post_temp;
    }
}
$theme = str_replace($post, $posts, $theme);  
4

1 回答 1

0

str_replace

str_replace — 用替换字符串替换所有出现的搜索字符串

这意味着,所有[post_tag]s 都将替换为第一个$tags值。每次跟随$tags都没有效果。

于 2013-04-06T21:17:29.400 回答