dongzhuo8210 2014-04-15 08:05
浏览 47
已采纳

使用str_replace将div添加到所有解析器结果

I'm using this string to get what's needed:

echo "<div id=\"lot\">".$table->children(2)->children(0)->plaintext."</div>";

And I get the result: 01 02 03 04 05 06 10 15 17 27 31 etc.

What I need is to add <div id=\"new\"></div> to each number.

<div id=\"new\">01</div> <div id=\"new\">02</div> etc etc.

I tried to use the code below but it doesn't work

 $table = $box_rezult->find("table.last-d", 0);
        $results = $table->children(2)->children(0)->plaintext;
        $string = $results;
        $var1 = str_replace(" ", "<div id=\"new\"> </div>", $string);
        echo $var1;

I would appreciate any help, thanks!

Thanks to everyone!!

  • 写回答

3条回答 默认 最新

  • doutu6658 2014-04-15 08:16
    关注

    explode() the string to an array, then implode() it:

    echo '<div class="new">' . implode('</div><div class="new">', explode(' ', $string)) . '</div>';
    

    I've changed id to class as id's must be unique.

    Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?