Textpattern Forum

You are not logged in. Register | Login | Help

#1 2008-12-02 14:31:50

ruud
Developer emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 4,571
Website

rvm_substr (v0.3)

This plugin allows you to cut a string of XHTML at a specific character limit, without causing invalid XHTML or utf8, unlike the sab_substr plugin which is meant for non HTML encoded plain text only.

Download

Features:

  • XHTML tag aware: tags are ignored when calculating the length for cutting the string, but are preserved and closed properly.
  • UTF8 aware: the length limit refers to the number of characters, not the number of bytes (which is typically greater when using a multibyte character set)
  • HTML escape code aware: something like & is counted as one character instead of four.
  • Automatically adds an ellipsis when the original string length exceeds the specified limit.

Changelog:

  • version 0.1 (2008-03-18)
  • version 0.2 (2008-12-02)
    • fix missing ellipsis when dealing with strings without tags (thanks wet/Robert)
    • corrected typo in documentation (thanks Thomas)
  • version 0.3 (2008-12-10)
    • fix incorrectly auto-closed tags when the tag element was over 1 char long (thanks wet/Robert)

Offline

#2 2008-12-10 09:50:16

wet
Developer
From: Lenzing, Austria
Registered: 2005-06-06
Posts: 3,119
Website

Re: rvm_substr (v0.3)

Looks like this (yummy) testcase lets rvm_substr lose its ability to close tags:

$in = <<<EOT
	<h3>Zutaten für zwei Portionen</h3>

	<ul>
		<li>150 g Dinkelflocken</li>
		<li>3 Eier</li>
		<li>150 g Emmentaler Käse</li>
		<li>Salz und Pfeffer</li>
		<li>3 Jungzwiebel</li>
		<li>Eine Knoblauchzehe</li>
	</ul>


	<h3>Zubereitung</h3>

	<ul>
		<li>Jungzwiebel hacken und anschwitzen</li>
		<li>Käse reiben und mit den Eiern, den Dinkelflocken und den Jungzwiebeln zu einer Teigmasse vermengen</li>
		<li>Die Knoblauchzehe in die Dinkelmasse drücken</li>
		<li>Die Masse salzen und pfeffern und danach 15 Minuten ruhen lassen</li>
		<li>Vier Laibchen formen und in goldbraun braten</li>
	</ul>
EOT;

$out = rvm_substr(array('length' => 350), $in);

Result in $out:

<h3>Zutaten für zwei Portionen</h3>

	<ul>
		<li>150 g Dinkelflocken</li>
		<li>3 Eier</li>
		<li>150 g Emmentaler Käse</li>
		<li>Salz und Pfeffer</li>
		<li>3 Jungzwiebel</li>
		<li>Eine Knoblauchzehe</li>
	</ul>


	<h3>Zubereitung</h3>

	<ul>
		<li>Jungzwiebel hacken und anschwitzen</li>
		<li>Käse reiben und mit den Eiern, den Dinkelflocken und den Jungzwiebeln zu einer Teigmasse vermengen</li>
		<li>Die Knoblauchzehe in die Dinkelmasse drücken</li>
		<li>Die Masse salzen und pfeffern und danach 15…</i></l>

NB: the closing tags at the very end.

Expected result:

Offline

#3 2008-12-10 20:57:04

ruud
Developer emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 4,571
Website

Re: rvm_substr (v0.3)

Thanks. Version 0.3 uploaded. It just needed a ) moved one character. The buggy version actually showed me something interesting. If you use a multi-matching () in PCRE, the last match is remembered. Not sure if that’s ‘last’ in a string-length context or ‘last’ in the actual pattern matching routine, although the latter is more likely.

Offline

#4 2008-12-21 23:34:25

maxvoltar
Member
From: Machelen, Belgium
Registered: 2007-08-16
Posts: 76
Website

Re: rvm_substr (v0.3)

Looks like there’s still a small bug in it:

<txp:link_to_prev><txp:rvm_substr length="20"><txp:prev_title /></txp:rvm_substr></txp:link_to_prev>
Results in: “The power and proble”
Instead of: “The power and proble…”

Any idea what might be causing this?


Textpattern projects: Maxvoltar, Made by Elephant, Twunch

Offline

#5 2008-12-22 11:36:46

ruud
Developer emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 4,571
Website

Re: rvm_substr (v0.3)

Tim, did you install the latest version (0.3) of this plugin?
I just tested it and if the string is indeed longer than what is shown (for example: “The power and problem”), the ellipsis is added.

Offline

#6 2008-12-22 11:44:54

maxvoltar
Member
From: Machelen, Belgium
Registered: 2007-08-16
Posts: 76
Website

Re: rvm_substr (v0.3)

I’ll update it!


Textpattern projects: Maxvoltar, Made by Elephant, Twunch

Offline

#7 2008-12-30 08:23:38

Kjeld
Member
From: Tokyo, Japan
Registered: 2005-02-05
Posts: 174
Website

Re: rvm_substr (v0.3)

Nice plugin, Ruud!

I am looking for the option to have it cut off text before a certain character, instead of a specific character limit. I would like to select the first word (the one before the first comma) in the list of keywords for an article. Would that be possible to do?


Photojournalist.jp – News and photos (100% txp)
JapaneseStreets.com – Japanese street fashion (mostly txp)
Old Photos of Japan – Japan between 1860 and 1940 (100% txp)
MeijiShowa – Stock photos of Japan between 1860 and 1940 (100% txp)

Offline

#8 2008-12-30 09:53:46

ruud
Developer emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 4,571
Website

Re: rvm_substr (v0.3)

Kjeld, that’s outside the scope of this plugin, but you can easily do it like this (not tested):

<txp:php>
  // turn string of keywords into an array
  $keywords = do_list($GLOBALS['thisarticle']['keywords']);
  // if keywords exist, output the first keyword only
  if ($keywords) echo htmlspecialchars($keywords[0]);
</txp:php>

Offline

#9 2009-11-08 19:42:41

Jaro
Member
From: S/F
Registered: 2004-11-18
Posts: 89

Re: rvm_substr (v0.3)

Thanks for the plugin, Ruud.

I’m getting some extra html entities after the truncated title:

My Title&#160;...

And I’m using this setup:

<txp:permlink><txp:sab_substr limit="35" trail="..."><txp:title /></txp:sab_substr></txp:permlink>

Last edited by Jaro (2009-11-08 19:45:04)

Offline

#10 2009-11-08 20:35:45

ruud
Developer emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 4,571
Website

Re: rvm_substr (v0.3)

You posted in the right topic, but you’re using the wrong plugin ;)
Try rvm_substr instead of sab_substr

Offline

Board footer

Powered by FluxBB