【问题标题】:Remove portion of string PHP删除字符串 PHP 的一部分
【发布时间】:2017-05-11 11:04:12
【问题描述】:

我正在处理一些 RSS 数据。我正在尝试删除字符串的某些部分。

字符串是这样的:

<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,并希望将值存储为变量。

非常感谢您的帮助。

【问题讨论】:

标签: php string loops rss simplexml


【解决方案1】:

试试这个

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      使用strip_tags,如果要保留p标签,添加第二个参数&lt;p&gt;

          <?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>');
      

      和输出:

      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>
      

      【讨论】:

        【解决方案4】:

        我认为您正在寻找 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";
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多