Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从字符串中获取一个标记?_Php_String_Image_Dom_Stripping - Fatal编程技术网

Php 从字符串中获取一个标记?

Php 从字符串中获取一个标记?,php,string,image,dom,stripping,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.

我有这种绳子

$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='Lorem Ipsum只是印刷和排版行业的虚拟文本。自16世纪以来,Lorem Ipsum一直是行业标准的虚拟文本,当时一位不知名的印刷商拿起一个打印工具,将其拼凑成一本打印样本书。它不仅存活了五个世纪,而且还跨越到电子排版,基本上保持不变。它在20世纪60年代随着包含Lorem Ipsum段落的Letraset表单的发布而流行,最近随着Aldus PageMaker等桌面出版软件的发布而流行,其中包括Lorem Ipsum版本。自15世纪以来,该行业的标准虚拟文本,当一个不知名的印刷商拿着一个铅字厨房,把它拼凑成一本铅字样本书时。它不仅存活了五个世纪,而且还跨越到了电子时代;
我想要的是从$string中获取第一个img,然后得到这样的新字符串

$newstring='<img src="http://www.yourwebite.com/images/assets/livechat-off.png" alt="Our products"/>';
$newstring='';
这样我就可以从字符串中只取一张图片,并将其显示在任何地方,这是可能的吗

尝试
strip_标记($string,”)但这样我就得到了所有的IMG标记,我只想从字符串中得到一个,可能是随机的,不想成为第一个:)

刚刚在PeeHaa的帮助下完成了功能,txanks mate:)

公共函数getimagefromstring($string)
{
$dom=新的DOMDocument();
//将字符串作为HTML文档加载
$dom->loadHTML($string);
$xpath=newdomxpath($dom);
//匹配文档中找到的第一个图像标记
$node=$xpath->query('//img[1]');
//没有找到图像
如果(!$node->length){
$image='Hello World';
}
其他的
{
//构建图像属性
$attrs='';
如果($node->item(0)->hasAttributes()){
foreach($node->item(0)->属性作为$attribute){
$attrs.=''.$attribute->name'.='.$attribute->value'.';
}
}
//显示图像
$image='';
}
返回$image;
}

您可以使用
preg\u match
查找标记

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

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

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

以下功能将为您节省大量时间。我必须试试

 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 . "/>";
函数提取器($str,$from,$to)
{
$from_pos=strpos($str$from);
$from_pos=$from_pos+strlen($from);
$to_pos=strpos($str,$to,$from_pos);//to必须在from之后
$return=substr($str,$from_pos,$to_pos-$from_pos);
未设置($str、$from、$to、$from_pos、$to_pos);
return$return;
}   
$extracted_str=提取器($string,“”);
回声“;

您尝试过什么?您认为您需要一个regexp还是一个简单的strps/substr就足够了;但这样我得到了所有的IMG标签,我只想从字符串中得到一个,也许是随机的,不是第一个:)这很好,但我有问题,如果字符串没有Txanks,你是最棒的:)没关系,但问题是我不知道字符串中是否有标签?你尝试过我的解决方案吗?因为它会检查字符串中是否有图像,否则会回退。上面的内容看起来非常脆弱。我的意思是像这样的脆弱会经常中断。
<?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";
 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 . "/>";