doulong4169 2016-01-02 04:41
浏览 46

如何从php中的字符串中删除html标签

I'm working with wordpress. I tried to display rss feeds from different sites. I put the url for each site as post content with category name rssfeed. Code for rss feed is given below.

<?php // Get RSS Feed(s) 
$arr = array('posts_per_page' => '10', 'category_name'=>'rssfeed',     'order' => 'ASC', 'category_id' =>21);
 $rssfeed = get_posts($arr);
  foreach($rssfeed as $rf)
  {
 include_once( ABSPATH . WPINC . '/feed.php' );

 // Get a SimplePie feed object from the specified feed source.
  $rss = fetch_feed( $rf->post_content);

 if ( ! is_wp_error( $rss ) ) : // Checks that the object is created correctly

 // Figure out how many total items there are, but limit it to 5. 
  $maxitems = $rss->get_item_quantity( 2 ); 

   // Build an array of all the items, starting with element 0 (first   element).
   $rss_items = $rss->get_items( 0, $maxitems );

endif;
?>

<ul>
 <?php if ( $maxitems == 0 ) : ?>
    <li><?php _e( 'No items', 'my-text-domain' ); ?></li>
<?php else : ?>
    <?php // Loop through each feed item and display each item as a hyperlink. ?>
    <?php foreach ( $rss_items as $item ) : ?>

        <li><div class="news">
           <div class="row"> 
            <div class="col-sm-6 news_content">

                <h3><strong><a href="<?php echo esc_url( $item->get_permalink() ); ?>"
                    title="<?php printf( __( 'Posted %s', 'my-text-domain' ), $item->get_date('j F Y | g:i a') ); ?>">
                    <?php echo esc_html( $item->get_title() ); ?>

                </a></strong></h3><br />
                <?php $cont=esc_html( $item->get_content() ); $vtag=substr($cont,0,100);
                /* $out=strip_tags($vtag,'<a>');
                 $result=str_replace('<span>',"Peter",$out);*/
                // var_dump($vtag);
                // $result=preg_replace('/ <p>/', ' ',$vtag);
                remove_accents ($vtag);
 ?> 

                 <p><?php echo $vtag;?></p>

            </div>
          </div>
         </div></li>

    <?php endforeach; ?>
<?php endif; ?>
</ul>

When I'm displaying a rss feed I'm getting the string like this as short content.

 <p>EGYPTAIR has signed a five-year contract with air transport communications specialist
  <div class="node-body magazine_issue-body"></div> <div class="fiel
ANA May Order A380s
 <div class="node-body article-body"><p>All Nippon Airways has reportedly agree
 <span>IATA welcome the historic COP 21 Paris Agreement which will provide additional 

I would like to get the string without <p>, <span>, <div class="node-body magazine_issue-body"></div>, <div class="fiel etc.
I tried preg_replace(), str_replace(), and strip_tags()

Any other way?

  • 写回答

1条回答 默认 最新

  • dqbjvg2518 2016-01-02 04:58
    关注

    The problem is that preg_replace is greedy and it takes up as much of a string as possible. The way I do it (although there is probably a great regexp that I just don't know about) is to break this up into a few steps.

    Step #1 - break everything up according to the "<" symbol. Since, in HTML, you can't start anything with the "<" (you have to use the &lt; command) - you should be able to break each HTML command onto it's own line. Like so:

    $a = explode( "<", $html );
    

    Step #2 - Remove everything to the first ">".

    foreach( $a as $k=>$v ){ $a[$k] = preg_replace( "/^.*>/", "", $v, 1); }
    

    Step #3 - Recombine everything

    $html = implode( " ", $a );
    

    Notes: Remember that the ">" can be used in a sentence - unlike the "<". So you want to make sure you only do the FIRST occrrance - which is what the "1" is for. By doing it this way you will always have only one HTML command per line. Enjoy! :-)

    Now - what happens if what you get is pure text and no HTML? Do a test to see if it contains a "<" in it followed by a word and not a number. Like so:

    if( !preg_match("/<\s*\d/", $html) ){ ...do something... }
    

    If you have the above - then it is just plain text coming in. Otherwise it will be HTML because you can't have something like "If A < 5 THEN..." it would have to be "If A &lt; 5 THEN...". Or you can check for some HTML command like the "<p>" command. Anything just to ensure you are getting HTML instead of just text.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大