node_teaser is bass_ackwards

Work | Nov. 12, 2008

After getting all apoplectic at one commenter foolish enough to support Joomla, I hasten to add that Drupal is no walk in the park either. I’m having a little issue with node_teaser in particular. Rather than use strrpos to search for the last occurrence of a character in a string, Drupal instead reverses the entire string and searches for a backwards string using strpos.

foreach ($points as $point => $offset) {
// The teaser is already reversed, but the break point isn't.
$rpos = strpos($reversed, strrev($point));
if ($rpos !== FALSE) {
$min_rpos = min($rpos + $offset, $min_rpos);

I am dumbfounded. I’m also writing my own node_teaser now, one that ignores HTML tags when determining teaser length, so your teaser isn’t comprised mostly of invisible <a href=””> code that does nothing for the reader.

UPDATE: A better programmer than myself informed me that — counter-intuitive though it may be — it’s actually faster in PHP to reverse a string and search a reversed piece of text through it than to use strrpos to search from back to front. Personally, I don’t see how it’s more efficient to use two functions to search for a string instead of one, and to keep two blocks of text in memory instead of one, but I’m going to take his word for it.


Comments are closed.