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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?