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

时间:2013-08-06 09:32:27

标签: php string image dom stripping

我有这种字符串

$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';

我想要的是从$ string获取第一个img并获得像这样的新字符串

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

这样我只能从字符串中取出一张图片并将其显示在任何地方,这是可能的吗?

尝试strip_tags($string, '<img>');,但这样我得到了所有IMG标签,我只想要一个来自字符串,也许是随机的,不是第一个:)

在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 个答案:

答案 0 :(得分:2)

使用PHP中的DOMXPath类很容易做到:

<?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) . "\r\n";
echo getImage($stringWithoutImage) . "\r\n";

演示:https://eval.in/40828

答案 1 :(得分:0)

您可以使用preg_match查找标记。

preg_match('/(<img[^>]+>)/i', $str, $matches)

字符串包含属性:$matches[1]

现在将src与其他标签分开

preg_match('/(src="[^"]+")/i', $matches[1], $matches)

该字符串包含url:$matches[1]

答案 2 :(得分:0)

以下功能可以节省您的大量时间。必须尝试

 function extractor($str,$from,$to)
 {
      $from_pos = strpos($str,$from);
      $from_pos = $from_pos + strlen($from);
      $to_pos   = strpos($str,$to,$from_pos);// to must be after from
      $return   = substr($str,$from_pos,$to_pos-$from_pos);
      unset($str,$from,$to,$from_pos,$to_pos );           
      return $return;

}   

$extracted_str = extractor($string,"<img","/>");

echo "<img" . $extracted_str . "/>";