简体   繁体   中英

Remove text from RSS description that is not within a tag element

I'm looking to remove text from an RSS description that is not within a tag element (ie. strong, p, span, etc.) I was able to remove a strong element, but I'm unable to target the "Location" text as it is not within a tag.

To clarify, I'm trying to remove

21.671N 158.117W or 5 nautical miles N of search location of 21.5928N 158.1034W.

I can't target a string because it will be different for each feed. Also, I tried applying a span tag to any text with no luck.

Below is my code...

<?php
  $rss = simplexml_load_file('http://www.ndbc.noaa.gov/rss/ndbc_obs_search.php?lat=21.5928&lon=-158.1034&radius=100');

  $i = 0;
  foreach($rss->channel->item as $item) {
    echo "<p>" . $item->description . "</p>";
    echo '<h2><a style="font-size:12px; text-decoration:none;" href="'. $item-      >link .'">' . $item->title . "</a></h2>";
    $i++;
    if ($i >= 1){
      break;
    }
  }
?>
<script>
  document.getElementsByTagName("strong")[1].setAttribute("hidden", true);
</script>

Thanks in advance for any help.

It seems like this feed always has the 'location' item as the second line; you could split on <br /> , remove the second line, then join back together with <br /> again.

eg

foreach($rss->channel->item as $item) {
  // Split on <br />
  $descriptionBits = explode("<br />", $item->description);
  // Remove the location line
  unset($descriptionBits[1]);
  // Glue the bits back together again
  $descriptionString = implode("<br />", $descriptionBits);

  // Then print it out
  echo "<p>" . $descriptionString . "</p>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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