为iframe编号并显示它

时间:2015-06-14 10:24:45

标签: php html string iframe str-replace

我有一段代码从输入字符串中读取空格分隔的URL,并用iframe视频替换它们。我想在每个上面插入iframe的编号。

以下是我的代码:

$result = $conn - > query($sql);
if ($result - > num_rows > 0) {
    // output data of each row
    while ($row = $result - > fetch_assoc()) {
        $nr = $row['nr'];
        $playery = $row['player'];
        $nrplayer = $nrplayer++; //old int (not used)


        //////remove from string all iframes and replace tons spaces to only one before every url (That makes string more clear)//////
        while (strpos($playery, ' ') !== false) {
            $playery = str_replace(" ", "", $playery);
        }
        $playery = str_replace("http", " http", $playery);
        $playery = str_replace('<iframewidth="420"height="315"src="', ' ', $playery);
        $playery = str_replace('<iframesrc="', ' ', $playery);


        ////////remove space of front and in the back ////////
        while (mb_substr($playery, 0, 1) == " ") {
            $playery = substr($playery, 1);
        }
        while (mb_substr($playery, 0, -1) == " ") {
            echo "ok";
            $playery = substr($playery, 1);
        }


        //////add some ads on page////////
        require("ads.php");

        //////create iframes///////
        $nrr = 1;
        echo $nrr."<br><br><br> <iframe allowfullscreen='1' width='800' height='500' src='".str_replace(' ', "'></iframe> ".$nrr++." <br><br><br> <iframe  allowfullscreen='1' width='800' height='500' src='", $playery)."'>";

    }
}

我只有1行,字符串$playery的值是

$playery = "http://www.dailymotion.com/swf/x2t2mgd http://www.dailymotion.com/swf/x2t2ury https://drive.google.com/file/d/0B4DzuaeCNhcsdlhnLURaU0dBQTg/preview https://drive.google.com/file/d/0B6ORWPhIsXTCUVpMd0RNTFU3Wk0/preview https://drive.google.com/file/d/0B9x5eo1ro_r9UVZZSVJucGtJbGs/preview http://mp4upload.com/embed-t9ve1iot81so.html"

使用我的代码,我得到的输出是这样的:

   1
<video>
   1
<video>
   1
<video>
   1
<video>

但是,我希望得到这个:

   1
<video>
   2
<video>
   3
<video>
   4
<video>

2 个答案:

答案 0 :(得分:0)

做一些像这样的事情,我想你是在你的循环中放入$ nrr = 1所以每次循环运行它变成1。

    <?php

$result = $conn->query($sql);
if ($result->num_rows > 0) {
    $nrr = 1;
    // output data of each row
    while ($row = $result->fetch_assoc()) {
        $nr = $row['nr'];
        $playery = $row['player'];
        $nrplayer = $nrplayer++;
        while (strpos($playery, ' ') !== false) {
            $playery = str_replace(" ", "", $playery);
        }
        $playery = str_replace("http", " http", $playery);
        $playery = str_replace('<iframewidth="420"height="315"src="', ' ', $playery);
        $playery = str_replace('<iframesrc="', ' ', $playery);
        while (mb_substr($playery, 0, 1) == " ") {
            $playery = substr($playery, 1);
        }
        while (mb_substr($playery, 0, -1) == " ") {
            echo "ok";
            $playery = substr($playery, 1);
        }
        require("ads.php");        
        echo $nrr . "<br><br><br> <iframe allowfullscreen='1' width='800' height='500' src='" . str_replace(' ', "'></iframe> " . $nrr++ . " <br><br><br> <iframe  allowfullscreen='1' width='800' height='500' src='", $playery) . "'>";
    }
}
?>

答案 1 :(得分:0)

在php $ var ++ / $ var--表达式在它被封装的语句之后执行,所以$ nrr在你的echo之后才会增加

$var = 1;
echo $var++; //1
echo $var; //2