从字符串中分离文本和图像,并将文本换行到<;p>;标记php

从字符串中分离文本和图像,并将文本换行到<;p>;标记php,php,Php,输入 $str = '<img src="http://www.blogcdn.com/www.engadget.com/media/2013/10/ipad-air-hands-on-angle.jpg"> Apple's plan to launch the iPad Air with cellular data support for T-Mobile was apparently just the start of a larger strategy. US Cellular

输入

$str = '<img src="http://www.blogcdn.com/www.engadget.com/media/2013/10/ipad-air-hands-on-angle.jpg"> Apple's plan to launch the iPad Air with cellular data support for T-Mobile was apparently just the start of a larger strategy. US Cellular has announced that it will offer the featherweight tablet on November 8th, while regional carriers like Bluegrass Cellular, C Spire and GCI say that they'll ...'

$str2 = '<img src="http://www.blogcdn.com/www.engadget.com/media/2013/10/ipad-air-hands-on-angle.jpg">Apple's plan to launch the iPad Air with cellular data support for T-Mobile was apparently just the start of a larger strategy. US Cellular has announced that it will offer the featherweight tablet on November 8th, while regional carriers like Bluegrass Cellular, C Spire and GCI say that they'll ...'
$str=”苹果计划为T-Mobile推出支持手机数据的iPad Air,这显然只是一个更大战略的开始。美国蜂窝已宣布将于11月8日推出轻量级平板电脑,而蓝草蜂窝、C Spire和GCI等地区运营商表示他们将
$str2=”苹果计划推出支持T-Mobile手机移动数据的iPad Air,这显然只是一个更大战略的开始。美国蜂窝已宣布将于11月8日推出轻量级平板电脑,而蓝草蜂窝、C Spire和GCI等地区运营商表示他们将
我需要的输出

<img src="http://www.blogcdn.com/www.engadget.com/media/2013/10/ipad-air-hands-on-angle.jpg">
<p>Apple's plan to launch the iPad Air with cellular data support for T-Mobile was apparently just the start of a larger strategy. US Cellular has announced that it will offer the featherweight tablet on November 8th, while regional carriers like Bluegrass Cellular, C Spire and GCI say that they'll ...</p>

苹果为T-Mobile推出支持手机数据的iPad Air的计划显然只是一个更大战略的开始。美国蜂窝已宣布将于11月8日推出轻量级平板电脑,而蓝草蜂窝、C Spire和GCI等地区运营商表示他们将

如何从上述两个字符串中获得所需的输出?我试过下面的方法,但效果似乎不太好

            $item_content = str_replace('> ', '> <p>', $item_content); 
            $item_content = str_replace('>', '> <p>', $item_content); 
$item\u content=str\u replace(“>”、“>”、$item\u content);
$item_content=str_replace(“>”、“>”、$item_content);

感谢您的帮助。

使用
DOMDocument

$str = '<img ... /> text ...';

$html = new DOMDocument();
$html->loadHtml($str);

$img_tag = $html->getElementsByTagName('img')->item(0)->C14N();
$text = trim($html->textContent);
$str='text…';
$html=新的DOMDocument();
$html->loadHtml($str);
$img_tag=$html->getElementsByTagName('img')->item(0)->C14N();
$text=trim($html->textContent);
然后,您可以任意方式输出:

echo "{$img_tag}\n<p>{$text}</p>";
echo“{$img_tag}\n{$text}

”;
您可以使用正则表达式。它将是:

$str = '<img src="http://www.blogcdn.com/www.engadget.com/media/2013/10/ipad-air-hands-on-angle.jpg"> Apple\'s plan to launch the iPad Air with cellular data support for T-Mobile was apparently just the start of a larger strategy. US Cellular has announced that it will offer the featherweight tablet on November 8th, while regional carriers like Bluegrass Cellular, C Spire and GCI say that they\'ll ...';

if(preg_match('/^(<img[\S\s]+?>)([\S\s]*)$/i', $str, $matches)) {
    $img = $matches[1];
    $text = trim($matches[2]);
    echo "$img\n<p>$text</p>";      
}
$str=”苹果计划为T-Mobile推出支持手机数据的iPad Air,这显然只是一个更大战略的开始。美国蜂窝已宣布将于11月8日推出轻量级平板电脑,而蓝草蜂窝、C Spire和GCI等地区运营商表示他们将;
if(preg_match('/^()([\S\S]*)$/i',$str,$matches)){
$img=$matches[1];
$text=trim($matches[2]);
回显“$img\n$text

”; }
@Beardy检查
$html->getElementsByTagName('img')->项(0)
是否返回
NULL
。有了这些,你就可以解决这个问题了。