Vanilla 1 is no longer supported or maintained. If you need a copy, you can get it here.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Options

how do I limit the number of characters?

edited July 2006 in Vanilla 1.0 Help
Hi, this is a general query. I am pulling an rss feed from my vanilla chat site, over to another section of my site (a non vanilla section). A poster had posted the code below, which I amended, and put in my rss feed url. The feed now appears at the top of my right hand sidebar, as planned. http://www.love2escape.com/multitrackers/ But, I want to limit the number of characters that appears in each post in the feed. How would I do that? TIA Here is the existing code. <?php set_time_limit(0); $file = "http://www.love2escape.com/chat/search/?PostBackAction=Search&Keywords=&Type=Comments&btnSubmit=Search&Feed=RSS2"; $TOTALITEM_WANTED=5; //any number of links $rss_channel = array(); $currently_writing = ""; $main = ""; $item_counter = 0; function startElement($parser, $name, $attrs) { global $rss_channel, $currently_writing, $main; switch($name) { case "RSS": case "RDF:RDF": case "ITEMS": $currently_writing = ""; break; case "CHANNEL": $main = "CHANNEL"; break; case "IMAGE": $main = "IMAGE"; $rss_channel["IMAGE"] = array(); break; case "ITEM": $main = "ITEMS"; break; default: $currently_writing = $name; break; } } function endElement($parser, $name) { global $rss_channel, $currently_writing, $item_counter; $currently_writing = ""; if ($name == "ITEM") { $item_counter++; } } function characterData($parser, $data) { global $rss_channel, $currently_writing, $main, $item_counter; if ($currently_writing != "") { switch($main) { case "CHANNEL": if (isset($rss_channel[$currently_writing])) { $rss_channel[$currently_writing] .= $data; } else { $rss_channel[$currently_writing] = $data; } break; case "IMAGE": if (isset($rss_channel[$main][$currently_writing])) { $rss_channel[$main][$currently_writing] .= $data; } else { $rss_channel[$main][$currently_writing] = $data; } break; case "ITEMS": if (isset($rss_channel[$main][$item_counter][$currently_writing])) { $rss_channel[$main][$item_counter][$currently_writing] .= $data; } else { $rss_channel[$main][$item_counter][$currently_writing] = $data; } break; } } } $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); if (!($fp = fopen($file, "r"))) { die("could not open XML input"); } while ($data = fread($fp, 4096)) { if (!xml_parse($xml_parser, $data, feof($fp))) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); } } xml_parser_free($xml_parser); // output HTML if (isset($rss_channel["ITEMS"])) { if (count($rss_channel["ITEMS"]) > 0) { for($i = 0;$i < $TOTALITEM_WANTED;$i++) { if (isset($rss_channel["ITEMS"][$i]["LINK"])) { print ("\n<div class=\"itemtitle\"><a href=\"" . $rss_channel["ITEMS"][$i]["LINK"] . "\">" . $rss_channel["ITEMS"][$i]["TITLE"] . "</a></div>"); } else { print ("\n<div class=\"itemtitle\">" . $rss_channel["ITEMS"][$i]["TITLE"] . "</div>"); } print ("<div class=\"itemdescription\">" . $rss_channel["ITEMS"][$i]["DESCRIPTION"] . "</div>"); } } else { print ("<b>There are no articles in this feed.</b>"); } } ?></i>

Comments

  • Options
    digifaddigifad New
    edited July 2006
    Change the following line:
    print ("<div class=\"itemdescription\">" . $rss_channel["ITEMS"][$i]["DESCRIPTION"] . "</div>"); }
    to:
    print ("<div class=\"itemdescription\">"); if (strlen($rss_channel["ITEMS"][$i]["DESCRIPTION"]) > 80) { print (substr($rss_channel["ITEMS"][$i]["DESCRIPTION"], 0, 77) . "..."); }else{ print ($rss_channel["ITEMS"][$i]["DESCRIPTION"]); } print ("</div>"); }
  • Options
    digifad, thank you, thank you, thank you, for the above code! I have my code amended now and it works a treat! I'd buy you a beer if I could. All my best wishes.
  • Options
    y.w. just chug one for me!
  • Options
    LOL will do! Guinness is on the menu here tonight (I am in Ireland)
  • Options
    The above script works beautifully - pulling the latest chat posts from my vanilla site over onto the right hand column of my blog site. But the script doesn't ignore new paragraphs when it pulls to first x number of characters from each post. Anyone know whatto place in the above code to make it ignore new paragraphs? here is the url...see the top right column, to see an example of a row being left due to a new paragraph in the chat post. http://www.love2escape.com/multitrackers/
This discussion has been closed.