【问题标题】:get dynamic variable embed into a string of html url and get the html code of it获取嵌入到 html url 字符串中的动态变量并获取它的 html 代码
【发布时间】:2012-02-15 02:16:13
【问题描述】:

有人可以帮我获取页面的代码吗?我使用的代码是:

<?php
$v='bla bla';
$j=2;
$x='';
$h=str_replace($x,$v,"http://www.youtube.com/watch?v=$x&list=blabla&index=$j++&feature=plpp_video");
$html = file_get_contents($h);
echo $html;
?>

wamp 正在抛出类似的错误

Warning: file_get_contents(http://www.youtube.com/watch?v=&list=blabla&index=2&feature=plpp_video)
[function.file-get-contents]: failed to open stream:
HTTP request failed! HTTP/1.0 404 Not Found in C:\wamp\www\php trails\trails\new 8.php on line 6

如果 x 没有被声明为 null,它会说:

Notice: Undefined variable: x in C:\wamp\www\php trails\trails\new 8.php on line 5

【问题讨论】:

    标签: php html youtube


    【解决方案1】:

    http://www.youtube.com/watch? v=& list=blabla&index=2&feature=plpp_video 不是有效的网址

    我想你想要这样的东西

    $v='bla_bla';
    $j=2;
    
    $h = "http://www.youtube.com/watch?v=".$v."&list=blabla&index=".($j++)."&feature=plpp_video";
    $html = file_get_contents($h);
    echo $html;
    

    编辑:从字符串中排除 $v 以突出显示更改

    【讨论】:

    • 谢谢老板,但是根据您建议的更改,我完成了一些工作,但我的主要目标是通过我获得的代码提取每个视频的标题和描述,我在哪里've feed 是包含所有视频的播放列表的页面
    【解决方案2】:

    当你只加入一些字符串时,不需要替换字符串。 您可以将变量作为 {var} 直接写入您的字符串。 确保你的变量是 url 编码的。

    <?php
    $v='bla bla';
    $j=2;
    $x='';
    $h="http://www.youtube.com/watch?v={$x}&list=blabla&index={$j}++&feature=plpp_video");
    $html = file_get_contents($h);
    echo $html;
    ?>
    

    【讨论】:

      【解决方案3】:

      您误解了str_replace()strings 在 PHP 中的一般工作方式。

      试试这个:

      <?php
      
        $v = 'bla bla';
        $j = 2;
        $urlTemplate = 'http://www.youtube.com/watch?v=%x%&list=blabla&index=%j%&feature=plpp_video';
      
        $h = str_replace(array('%x%','%j%'), array($v,$j++), $urlTemplate);
        echo file_get_contents($h);
      

      当您将变量放入字符串时,它会在解析字符串时进行评估 - 在赋值时。您所做的将不起作用,因为该字符串将评估为 http://www.youtube.com/watch?v=&amp;list=blabla&amp;index=2++&amp;feature=plpp_video

      字符串中的变量不会创建对变量的引用,它们只是使用当前值。

      上面的代码使用占位符 %x%%j% 并将它们替换为您要使用的实际值。

      【讨论】:

      • 感谢老板,但是根据您建议的更改,我已经完成了一些工作,但我的主要目标是通过我获得的代码提取每个视频的标题和描述,我在哪里've feed 是包含所有视频的播放列表的页面
      【解决方案4】:

      我发现不需要处理 url 中的值,每个视频都带有唯一的 id,因此无需担心增加/更改索引值或播放列表值

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多