douzhi2988 2013-08-06 09:32
浏览 38
已采纳

从字符串中获取一个标签?

I have this kind of string

$string='Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type <img src="http://www.yourwebite.com/images/assets/livechat-off.png" alt="Our products"/> and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<img src="http://www.yourwebite.com/images/youareon.jpg" alt="Our products"/>the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic';

What i want is to take first img from $string and to get new string like this

$newstring='<img src="http://www.yourwebite.com/images/assets/livechat-off.png" alt="Our products"/>';

That way i can take just one picture from string and display it anywhere, is taht possible?

Tried strip_tags($string, '<img>'); but that way i got all IMG tags, i just want one from string, maybe random, does not to be the first :)

Just made function with help of PeeHaa, txanks mate :)

public function getimagefromstring($string) 

{
 $dom = new DOMDocument();
    // load the string as an HTML document
    $dom->loadHTML($string);

    $xpath = new DOMXPath($dom);

    // match the first image tag found in the document
    $node = $xpath->query('//img[1]');

    // no images found
    if (!$node->length) {
        $image='Hello World';
    }

    else 
    {
    // build the image attributes
    $attrs = '';
    if ($node->item(0)->hasAttributes()) {
        foreach ($node->item(0)->attributes as $attribute) {
            $attrs.= ' ' . $attribute->name . '="' . $attribute->value . '"';
        }
    }
    // display the image
    $image='<img' . $attrs . '>';   
    }

    return $image;   
}
  • 写回答

3条回答 默认 最新

  • dtslobe4694 2013-08-06 09:42
    关注

    This is easy to do with the DOM and XPath classes in PHP:

    <?php
    
    function getImage($html)
    {
        $dom = new DOMDocument();
        // load the string as an HTML document
        $dom->loadHTML($html);
    
        $xpath = new DOMXPath($dom);
    
        // match the first image tag found in the document
        $node = $xpath->query('//img[1]');
    
        // no images found
        if (!$node->length) {
            return '<img src="myimage">';
        }
    
        // build the image attributes
        $attrs = '';
        if ($node->item(0)->hasAttributes()) {
            foreach ($node->item(0)->attributes as $attribute) {
                $attrs.= ' ' . $attribute->name . '="' . $attribute->value . '"';
            }
        }
    
        // display the image
        return '<img' . $attrs . '>';
    }
    
    $stringWithImage ='Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type <img src="http://www.yourwebite.com/images/assets/livechat-off.png" alt="Our products"/> and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<img src="http://www.yourwebite.com/images/youareon.jpg" alt="Our products"/>the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic';
    
    $stringWithoutImage = 'Just some random string';
    
    echo getImage($stringWithImage) . "
    ";
    echo getImage($stringWithoutImage) . "
    ";
    

    Demo: https://eval.in/40828

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

报告相同问题?

悬赏问题

  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败