2016-12-27 11 views
0

私はいくつかのRSSデータを扱っています。私は文字列の特定の部分を削除しようとしています。文字列の一部を取り除くPHP

文字列は、このようなものです:

<description><![CDATA[<div style="float:right;margin:0 0 1ex 1ex"><a href="/link/123" rel="nofollow" title="Link: www.example.com/testing-information"><img src="http://www.example.com/a/100/1000.jpg?hash=a38232k" alt="More info here"/></a></div><p>THIS IS THE INFO I ACTUALLY WANT TO KEEP</p> 

私だけ表示したい:

**THIS IS THE INFO I ACTUALLY WANT TO KEEP** 

または

**<p>THIS IS THE INFO I ACTUALLY WANT TO KEEP</p>** 

私はPHPを使用していますが、として値を格納したいと思います変数。

ご協力いただき誠にありがとうございます。

+1

使用 'でstrip_tags($text)によってstrip_tags' –

+0

はXMLの一部のようですが、[simplexml_load_string](http://www.php.net/manual/en/function.simplexml-load-string.php)をフルストリング – bansi

答えて

0

使用strip_tags、あなたはpタグを維持したい場合は、二番目のパラメータを追加します<p>

<?php 
     //without p tag 
     echo strip_tags('<description><![CDATA[<div style="float:right;margin:0 0 1ex 1ex"><a href="/link/123" rel="nofollow" title="Link: www.example.com/testing-information"><img src="http://www.example.com/a/100/1000.jpg?hash=a38232k" alt="More info here"/></a></div><p>THIS IS THE INFO I ACTUALLY WANT TO KEEP</p>'); 
     echo "\n"; 
     //with p tag 
     echo strip_tags('<description><![CDATA[<div style="float:right;margin:0 0 1ex 1ex"><a href="/link/123" rel="nofollow" title="Link: www.example.com/testing-information"><img src="http://www.example.com/a/100/1000.jpg?hash=a38232k" alt="More info here"/></a></div><p>THIS IS THE INFO I ACTUALLY WANT TO KEEP</p>', '<p>'); 

と出力:

[email protected]:~$ php test.php 
THIS IS THE INFO I ACTUALLY WANT TO KEEP 
<p>THIS IS THE INFO I ACTUALLY WANT TO KEEP</p> 
0

この

$str = '<description><![CDATA[<div style="float:right;margin:0 0 1ex 1ex"><a href="/link/123" rel="nofollow" title="Link: www.example.com/testing-information"><img src="http://www.example.com/a/100/1000.jpg?hash=a38232k" alt="More info here"/></a></div><p>THIS IS THE INFO I ACTUALLY WANT TO KEEP</p>'; 
preg_match('~<p>(.*?)</p>~', $str, $matches); 
var_dump($matches); 
0

使用strip_tagsをお試しください:

$text = '<description><![CDATA[<div style="float:right;margin:0 0 1ex 1ex"><a href="/link/123" rel="nofollow" title="Link: www.example.com/testing-information"><img src="http://www.example.com/a/100/1000.jpg?hash=a38232k" alt="More info here"/></a></div><p>THIS IS THE INFO I ACTUALLY WANT TO KEEP</p>'; 
echo strip_tags($text); 

Working Demo

0

私はあなたが、あなたが行うことができますXHTMLタグ以外のテキスト情報を探していると思いますが、php-例 -

$text = '<description><![CDATA[<div style="float:right;margin:0 0 1ex 1ex"><a href="/link/123" rel="nofollow" title="Link: www.example.com/testing-information"><img src="http://www.example.com/a/100/1000.jpg?hash=a38232k" alt="More info here"/></a></div><p>THIS IS THE INFO I ACTUALLY WANT TO KEEP</p>'; 
echo strip_tags($text); 
echo "\n"; 
関連する問題