代码之家  ›  专栏  ›  技术社区  ›  user2828442

查找图像src并向图像标记添加前缀

php
  •  1
  • user2828442  · 技术社区  · 5 年前

    我有一个用于博客的字符串,这个字符串中可能有无限数量的图片,我要做的是获取 src="" 并添加 prefix url

    我的字符串:

    $test = 'Hello world
    <img src="images/image1.jpg" />Line 2
    <img src="images/image2.jpg" />Some text 
    <img src="images/image3.jpg" />';
    

    我可以加前缀 href . 我能够做到这一点:

    <a href="images/image1.jpg"><img src="images/image1.jpg" /></a>
    <a href="images/image1.jpg"><img src="images/image2.jpg" /></a>
    <a href="images/image1.jpg"><img src="images/image3.jpg" /></a>
    

     $new = preg_replace('/(<img[^>]+src="([^\\"]+)"[^>]+\\/>)/','<a href="\\2">\\1</a>',$test2);
        echo $new;
    

    我需要补充 foldername/ 作为所有图像的前缀 src . 我试图将此转换为以下内容:

    <a href="images/image1.jpg"><img src="foldername/images/image1.jpg" /></a>
    <a href="images/image1.jpg"><img src="foldername/images/image2.jpg" /></a>
    <a href="images/image1.jpg"><img src="foldername/images/image3.jpg" /></a>
    

    2 回复  |  直到 5 年前
        1
  •  3
  •   Nigel Ren    5 年前

    使用DOMDocument而不是regex来实现这一点(一个很好的原因是 https://stackoverflow.com/a/1732454/1213708 ).

    <img> 标签。它首先获取 src <a> 标记并添加(原始) src公司 href 属性。然后它取代了 < 用标记 <a> ,但将旧值添加回 <a>

    $dom = new DOMDocument();
    $dom->loadHTML($test, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
    
    foreach ( $dom->getElementsByTagName("img") as $image ) {
        $src = $image->getAttribute("src");
        $image->setAttribute("src", "foldername/".$src);
        $anchor = $dom->createElement("a");
        $anchor->setAttribute("href", $src);
        $image->parentNode->replaceChild($anchor, $image);
        $anchor->appendChild($image);
    }
    
    echo $dom->saveHTML();
    
        2
  •  0
  •   Aman Rawat    5 年前

    现在不重要了,但这里有一个没有regex的解析解决方案。我更喜欢@NigelRen解决方案,事实上,现在它已经发布了。但是,这里有一种方法可以构建一个没有regex的图像url列表,用于解决您的问题并提供进一步的探索。我还没有测试过代码,但我很公平。

    <?php
    
    $html = 'some html here';
    
    $sentinel = '<img src="';
    
    $quoteSentinel = '"';
    
    $done = false;
    
    $offset = 0;
    
    $imageURLS = array();
    
    while (!$done) {
    
        $nextImageTagPos = strpos($html, $sentinel, $offset);
    
        if ($nextImageTagPos !== false) {
    
            //*** Parsing
    
            // Find first quote
    
            $quoteBegins = false;
    
            for ($i = $nextImageTagPos; $i < $nextImageTagPos + 100; $i++) {
                if (substr($html, $i, 1) == $quoteSentinel) {
                    $quoteBegins = $i;
                    break;
                }
            }
    
            // Find ending quote
    
            $quoteEnds = false;
    
            if ($quoteBegins !== false) {
                for ($i = $quoteBegins + 1; $i < $quoteBegins + 1000; $i++) {
                    if (substr($html, $i, 1) == $quoteSentinel) {
                        $quoteEnds = $i;
                        break;
                    }
                }
    
                if ($quoteEnds !== false) {
    
                    // Hooray! now we are getting somewhere
    
                    $imgUrlLength = ($quoteEnds - $quoteBegins) - 1;
    
                    $imgUrl = substr($html, $quoteBegins + 1, $imgUrlLength);
    
                    // ***Requirements
    
                    /*
                    I have a string that is for a blog, potentially it could have an unlimited number of images in this string, what i am trying to do is get all of the src="" and add a prefix to the url and use it as the hyperlink.
                    */
    
                    // But for brevity lets skip requirements and build an array of URLs.
    
                    $imageURLS[] = $imgUrl;
    
                    $offset = $quoteEnds + 1;
    
    
    
    
                }
    
            }
    
            // Extract url
    
        }
        else {
            $done = true;
        }
    
    
    }