1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Wordpress feeds showing crazy characters

Discussion in 'PHP' started by MTTRSS, Feb 6, 2020.

  1. #1
    My Wordpress blog use the charset UTF-8 and when I try to display the feed through a RSS feed reader on my VBulletin forum (that is encoded with ISO-8859-1), the output show weird characters like: †etc. I tried to put this code: header('Content-Type: text/html; charset=VALUE'); in my feed_reader.php and fix the problem, I can see the feeds correctly but now all the vBulletin shows that crazy characters.

    How can I fix this? Thanks! :)

    This is my feed_reader.php by the way:
    <?php
    
    $url = "FEED URL";
    if(isset($_POST['submit'])){
       if($_POST['feedurl'] != ''){
         $url = $_POST['feedurl'];
       }
    }
    
    $invalidurl = false;
    if(@simplexml_load_file($url)){
      $feeds = simplexml_load_file($url);
    }else{
      $invalidurl = true;
      echo "<h2>Invalid RSS feed URL.</h2>";
    }
    
    
    $i=0;
    if(!empty($feeds)){
    
      $site = $feeds->channel->title;
      $sitelink = $feeds->channel->link;
    
    
      foreach ($feeds->channel->item as $item) {
    
       $title = $item->title;
       $title = substr($title, 0, 32)."";
       $link = $item->link;
       $description = $item->description;
       $postDate = $item->pubDate;
       $pubDate = date('D, d M Y',strtotime($postDate));
    
    
    
    
       if($i>=3) break;
      ?>
       <div class="post">
         <div class="post-head2">
           <h2 class="news_feed2"><a class="feed_title" href="<?php echo $link; ?>"><?php echo $title; ?></a></h2>
    
         </div>
         <div class="post-content2">
           <?php echo implode(' ', array_slice(explode(' ', $description), 0, 20)) . "..."; ?>
         </div>
       </div>
    
       <?php
        $i++;
       }
    }else{
       if(!$invalidurl){
         echo "<h2>No item found</h2>";
       }
    }
    ?>
    
    PHP:
     
    Solved! View solution.
    MTTRSS, Feb 6, 2020 IP
  2. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #2
    Is your blog written in English? Those characters generally come up when some other language is typed instead of English. At your end it will appear right because you got required charset, but third party websites will not display the content properly.
     
    JEET, Feb 7, 2020 IP
  3. MTTRSS

    MTTRSS Peon

    Messages:
    6
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    3
    #3
    @JEET

    Hey Jeet, the blog is written in Italian, but I fixed it just now by adding the "utf8_decode" to my loop. Since we are here I saw that in my feed beside title and description I also have the image tag, how can I add it to my reader? so I can show also a picture next the description.

    Thank you :)
     
    MTTRSS, Feb 7, 2020 IP
  4. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #4
    To get the contents of <image> tag, add this line just below the "$link" line:
    (I am assuming it has a URL of an image in it)


    $link = $item->link;
    $image= $item->image;


    To display the image somewhere in the reader, do something like this:


    <h2 class="news_feed2"><a class="feed_title" href="<?php echo $link; ?>"><?php echo $title; ?></a></h2>
    <p><img src="<?php echo $image; ?>" /></p>
     
    JEET, Feb 7, 2020 IP
  5. MTTRSS

    MTTRSS Peon

    Messages:
    6
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    3
    #5
    I edited the file with your instructions but I see no images :( When I analize the page it says: <img src(unknown)> where it should be the link of the image.


    In my feed the image is in this tag:

    <enclosure url="https://xtremehub.it/wp-content/uploads/2020/02/kfa-150x150.png" length="19983" type="image/png" />
     
    MTTRSS, Feb 7, 2020 IP
    JEET likes this.
  6. #6
    Ok, images are not inside a tag, but as an attribute.
    Do this:

    $link= $item->link;
    $image= $item->enclosure['url'];

    <h2 class="news_feed2"><a class="feed_title" href="<?php echo $link; ?>"><?php echo $title; ?></a></h2>
    <p><img src="<?php echo $image; ?>" /></p>
     
    JEET, Feb 8, 2020 IP
  7. MTTRSS

    MTTRSS Peon

    Messages:
    6
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    3
    #7
    Thank you JEET! Worked perfectly now! Best answer :)
     
    MTTRSS, Feb 9, 2020 IP
    JEET likes this.
  8. MTTRSS

    MTTRSS Peon

    Messages:
    6
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    3
    #8
    Thank you JEET! Worked perfectly now! Best answer :)
     
    MTTRSS, Feb 9, 2020 IP
    JEET likes this.
  9. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #9
    Welcome, glad to help :) Thanks for selecting as best answer :)
     
    JEET, Feb 9, 2020 IP