【问题标题】:php get URL of first img src inside of specified html elementphp获取指定html元素内第一个img src的URL
【发布时间】:2014-04-01 15:38:34
【问题描述】:

我正在尝试找到使用 PHP 获取指定元素/类中第一张图片的 URL 的最简单方法,以插入 Open Graph og:image 和 Twitter twitter:image

<figure class="pictures">
<img src="/pictures/1.jpg"> <!-- it would be this here -->
<img src="/pictures/2.jpg">
<img src="/pictures/3.jpg">
</figure>

<meta property="og:image" content="<?php echo $og_image; ?>"/>

我还想知道我是否可以让它通过回显在相对 URL 前面获取域,而不是在元标记中的回显前面硬编码它。

【问题讨论】:

    标签: php html facebook-opengraph


    【解决方案1】:

    试试这个:

    function gsb($string, $start, $end)
    {
        $string = " " . $string;
        $ini    = strpos($string, $start);
        if ($ini == 0)
            return "";
        $ini += strlen($start);
        $len = strpos($string, $end, $ini) - $ini;
        return substr($string, $ini, $len);
    }
    $pic=gsb($string,'src="','"');
    

    【讨论】:

    • 这里 $pic 包含所需的链接。
    【解决方案2】:

    我没有安装 PHP,所以我无法实际测试这段代码。但把它当作表达想法的伪代码。

    <?php
        echo "<figure class='pictures'>";
        $URLS = array("/pictures/1.jpg","/pictures/2.jpg","/pictures/3.jpg");
        for($i=0;$i<count($URLS);i++) {
            echo "<img src='$URLS($i)'>";
        }
        echo "</figure>";
        echo "<meta property='og:image' content='$URLS(0)' />";
    ?>
    

    这将为您提供所需的结果,使添加/删除 imgs 更容易,并允许您在代码朝该方向构建时通过算法生成 imgs 列表。

    要获取服务器的域,请查看 $_SERVER['HTTP_HOST']$_SERVER['SERVER_NAME']

    【讨论】:

    • 我认为这行不通。我仍然是编程的初学者,但我认为它必须是没有任何指定 URL 的东西,因为它必须适用于具有不同内容但在同一个元素/类中的许多页面。伪代码(显然不起作用,哈哈):&lt;?php get_url(img.src.1(class.figure.pictures)); ?&gt;。它必须能够在指定元素/类中获取并回显 img src 的第一个 URL,在本例中为 &lt;figure class="pictures"&gt;
    • 在这种情况下,您必须将文档作为 DOM 进行处理。根据我有限的理解,php 不理解它周围的 HTML 的上下文,因此您必须通过 PHP 传递整个页面,解析出您需要的内容,生成额外的代码,然后使用 echo 将其放入响应中.这是 HaRsH 推荐的。
    【解决方案3】:
        with javascript like this BUT
        **by using php FOLLOW THIS LINK**
        [http://stackoverflow.com/questions/10130858/get-img-src-with-php][1]
    
            //this is jquery code for finding the SRC of first imahe 
            <script>
            //$( document ).ready(function() {//here we are playing with image so use
             $( window ).load(function() {
    
    
            var image_url = $( ".pictures img:first-child" ).attr("src");
            //in variable image_url the url of the image
            alert(image_url);
    
            });
    
    
            </script>
    
    
          [1]: http://stackoverflow.com/questions/10130858/get-img-src-with-php
    
        USING PHP:-
    <?php     
    //i just copy that code from upper link and make small change
    
    
        //if you are generating these image by loop then generate $html variable and use this code
        $html = '<img src="/arrow_down.png"><img src="/arrow_down1.png"><img src="/arrow_down2.png">';
    
        $doc = new DOMDocument();
    
        $doc->loadHTML($html);
    
        $xpath = new DOMXPath($doc);
    
        echo $src = $xpath->evaluate("string(//img/@src)"); # "/images/image.jpg"
    ?>
    

    【讨论】:

    • 它必须是 PHP,因为 JavaScript 与抓取不兼容。但是,我不理解 PHP 示例,因为它没有显示任何针对特定元素/类的方式。
    • 你想要第一张图片,所以在 .php 文件中生成图片的条件
    • 可能是这段代码对你有帮助 loadHTML($html); $xpath = 新 DOMXPath($doc); echo $src = $xpath->evaluate("string(//img/@src)"); # "/images/image.jpg" ?>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-28
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多