简体   繁体   English

删除部分字符串PHP

[英]Remove portion of string PHP

I am working with some RSS data. 我正在处理一些RSS数据。 I am trying to remove certain parts of the string. 我正在尝试删除字符串的某些部分。

The string is like this: 字符串是这样的:

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

I only want to display: 我只想显示:

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

or 要么

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

I am using PHP and would like to store the value as a variable. 我正在使用PHP,并希望将值存储为变量。

Your help is greatly appreciated. 非常感谢您的帮助。

Try this 尝试这个

$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);

Use strip_tags : 使用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 工作演示

Use strip_tags , if you want keep the p tag, add the second parameter <p> 使用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>');

and the output: 和输出:

ei@localhost:~$ php test.php
THIS IS THE INFO I ACTUALLY WANT TO KEEP
<p>THIS IS THE INFO I ACTUALLY WANT TO KEEP</p>

I think you are looking for text information other than xhtml tags, you can do that by strip_tags($text) in php-example- 我认为您正在寻找除xhtml标记之外的文本信息,可以通过php-example-中的strip_tags($text)来实现。

$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";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM