Ik heb een vraagje waar ik niet helemaal uitkom. Ik weet namelijk helemaal niets van PHP, maar wil toch graag iets gedaan hebben. En het werkt al, maar tot op zekere hoogte.
Ik laat op mijn site in aanbouw een RSS-feed zien met SimplePie. Dat werkt goed, maar nu wil ik de titels na een aantal karakters afbreken om iedere titel op één regel te houden. Daar geeft de website van SimplePie wel een code voor, maar ik weet niet hoe ik die in mijn bestaande code moet plaatsen.
De nu werkende situatie ziet er zo uit:
De eerste regel op de site is:
<?php <br>include_once $_SERVER['DOCUMENT_ROOT'] . '/mapnaam/simplepie.inc'; <br>$feed = new SimplePie('http://www.telegraaf.nl/rss/index.xml');<br>?>
Op de plek waar ik de RSS-feed plaats staat deze code:
<ul id="rss"><?php foreach ($feed->get_items(0, 6) as $item): ?> <li> <?php echo $item->get_date('d-m-Y'); ?><a href="<?php print $item->get_permalink(); ?>"> <?php print $item->get_title(); ?></a> </li><?php endforeach; ?></ul>
<a href="<?php print $item->get_permalink(); ?>"> <?php print $item->get_title(); ?></a> </li><?php endforeach; ?></ul>
En dat levert de volgende fraaie RSS-feed op:
Nu zou ik met de volgende code in staat moeten zijn om de titels in te korten:
<?php<br>function shorten($string, $length)<br>{<br> // By default, an ellipsis will be appended to the end of the text.<br> $suffix = '…';<br> <br> // Convert 'smart' punctuation to 'dumb' punctuation, strip the HTML tags,<br> // and convert all tabs and line-break characters to single spaces.<br> $short_desc = trim(str_replace(array("\r","\n", "\t"), ' ', strip_tags($string)));<br> <br> // Cut the string to the requested length, and strip any extraneous spaces <br> // from the beginning and end.<br> $desc = trim(substr($short_desc, 0, $length));<br> <br> // Find out what the last displayed character is in the shortened string<br> $lastchar = substr($desc, -1, 1);<br> <br> // If the last character is a period, an exclamation point, or a question <br> // mark, clear out the appended text.<br> if ($lastchar == '.' || $lastchar == '!' || $lastchar == '?') $suffix='';<br> <br> // Append the text.<br> $desc .= $suffix;<br> <br> // Send the new description back to the page.<br> return $desc;<br>}<br>?>
SimplePie geeft ook nog een voorbeeld van hoe de bovenstaande code te gebruiken
<?php<br> <br>// Shorten the text to 150 characters.<br>echo shorten($item->get_description(), 150);<br> <br>?>
Kortom: waar plaats ik wat om deze situatie werkend te krijgen?
(Bewerkt door EdMac om 9:33, 16-02-2011)
php print $item->get_title();
vervangen door:
php print shorten($item->get_title(), 150);
en ervoor zorgen dat de functie shorten ook daadwerkelijk gevonden kan worden(aan de pagina toevoegen of include_once gebruiken als simplepie.inc in je voorbeeld).
Ik plaats nu deze code aan het begin van de pagina:
<?php <br>include_once $_SERVER['DOCUMENT_ROOT'] . '/mapnaam/simplepie.inc'; <br>$feed = new SimplePie('http://www.telegraaf.nl/rss/index.xml'); <br>?><?php <br>include_once function shorten($string, $length) <br>{ <br> // By default, an ellipsis will be appended to the end of the text. <br> $suffix = '…'; <p> // Convert 'smart' punctuation to 'dumb' punctuation, strip the HTML tags,<br> // and convert all tabs and line-break characters to single spaces. <br> $short_desc = trim(str_replace(array("\r","\n", "\t"), ' ', strip_tags($string))); <p> // Cut the string to the requested length, and strip any extraneous spaces<br> // from the beginning and end. <br> $desc = trim(substr($short_desc, 0, $length)); <p> // Find out what the last displayed character is in the shortened string <br> $lastchar = substr($desc, -1, 1); <p> // If the last character is a period, an exclamation point, or a question <br> // mark, clear out the appended text. <br> if ($lastchar == '.' || $lastchar == '!' || $lastchar == '?') $suffix=''; <p> // Append the text. <br> $desc .= $suffix; <p> // Send the new description back to the page. <br> return $desc; <br>} <br>?>
En roep de functie op deze manier aan:
<ul id="rss"><?php foreach ($feed->get_items(0, 6) as $item): ?> <li> <?php echo $item->get_date('d-m-Y'); ?><a href="<?php print $item->get_permalink(); ?>"> <?php print shorten($item->get_title(), 25); ?></a> </li><?php endforeach; ?></ul>
<a href="<?php print $item->get_permalink(); ?>"> <?php print shorten($item->get_title(), 25); ?></a> </li><?php endforeach; ?></ul>
Maar dat heeft deze foutmelding als resultaat:
Kan iemand mij zeggen hoe ik de functie 'shorten' op een juiste manier gevonden kan laten worden? Alvast bedankt!
include_once function shorten($string, $length)
De function include_once gebruik je alleen om een ander bestand met php code in te voegen. Dus als je de functie direct in het bestand invoegt, haal je uit de bovenstaande regel include_once weg.
<?php <br>include_once function shorten($string, $length)
<?php <br>function shorten($string, $length)
16-02-2011Dit is een koptekst over één regel
16-02-2011Dit is een tweede koptekst
EdMac om 10:57, 16-02-2011Vreemd. Wat ik ook probeer, hij blijft de error: Fatal error: Call to undefined function shorten() in.... geven. Op de één of andere manier lukt het me dus niet die functie in te voegen.
Aan het begin van de pagina (als eerste code):
<?php <br>include_once $_SERVER['DOCUMENT_ROOT'] . '/mapnaam/simplepie.inc'; <br>$feed = new SimplePie('http://www.telegraaf.nl/rss/index.xml');<br>?><?php <br>function shorten($string, $length) <br>{ <br> // By default, an ellipsis will be appended to the end of the text. <br> $suffix = '…'; <p> // Convert 'smart' punctuation to 'dumb' punctuation, strip the HTML tags, <br> // and convert all tabs and line-break characters to single spaces. <br> $short_desc = trim(str_replace(array("\r","\n", "\t"), ' ', strip_tags($string))); <p> // Cut the string to the requested length, and strip any extraneous spaces <br> // from the beginning and end. <br> $desc = trim(substr($short_desc, 0, $length)); <p> // Find out what the last displayed character is in the shortened string <br> $lastchar = substr($desc, -1, 1); <p> // If the last character is a period, an exclamation point, or a question <br> // mark, clear out the appended text. <br> if ($lastchar == '.' || $lastchar == '!' || $lastchar == '?') $suffix=''; <p> // Append the text. <br> $desc .= $suffix; <p> // Send the new description back to the page. <br> return $desc; <br>} <br>?>
Vervolgens op de plek waar ik de feed wil plaatsen:
<ul id="rss"> <?php foreach ($feed->get_items(0, 6) as $item): ?> <li> <?php echo $item->get_date('d-m-Y'); ?> <a href="<?php print $item->get_permalink(); ?>"> <?php print shorten($item->get_title(), 25); ?></a> </li> <?php endforeach; ?> </ul>
<a href="<?php print $item->get_permalink(); ?>"> <?php print shorten($item->get_title(), 25); ?></a> </li> <?php endforeach; ?> </ul>