2016-12-27 15 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' –

+0

выглядит как часть XML, попробуйте использовать [simplexml_load_string] (http://www.php.net/manual/en/function.simplexml-load-string.php) в полной строке – bansi

ответ

0

Используйте strip_tags, если вы хотите сохранить р тег, добавить второй параметр <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 тегов, вы можете сделать это с помощью strip_tags($text) в 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";