0

入力

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

必要な出力

<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>

上記の 2 つの文字列から必要な出力を取得するにはどうすればよいですか? 私は次のことを試しましたが、これはそれほど素晴らしいものではないようです。

            $item_content = str_replace('> ', '> <p>', $item_content); 
            $item_content = str_replace('>', '> <p>', $item_content); 

助けていただければ幸いです。

4

2 に答える 2

1

正規表現を使用できます。そうなる:

$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>";      
}
于 2013-10-27T13:20:19.353 に答える