Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2009-11-02 13:30:19

beechy
Member
Registered: 2006-03-02
Posts: 92

Limit characters in body

I know there are a few plugins to do this such as rss_auto_excerpt

However I am currently using the following code from a forum post to limit the characters in my excerpts which works nicely.

<txp:php>
	$limit = 100;
	$bod = strip_tags(excerpt());
	$bod = preg_replace('/^(.{'.$limit.'}).*$/u', '$1', $bod);
	echo $bod;
</txp:php>


I was hoping I could use the same code for the article body...

But when I change the above to
<txp:php>
	$limit = 100;
	$bod = strip_tags(body());
	$bod = preg_replace('/^(.{'.$limit.'}).*$/u', '$1', $bod);
	echo $bod;
</txp:php>


It simply display all the text from the body. How do I get this to work for the  <txp:body /> tag

Last edited by beechy (2009-11-02 13:32:55)

Offline

#2 2009-11-02 13:33:46

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: Limit characters in body

Try using body_html instead of body.


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#3 2009-11-02 14:46:40

beechy
Member
Registered: 2006-03-02
Posts: 92

Re: Limit characters in body

Hi Maniqui,

thanks for your help

If I use body_html i get the following error…

Fatal error: Call to undefined function body_html() in /html/textpattern/publish/taghandlers.php(3127) : eval()’d code on line 3

Offline

#4 2009-11-02 15:37:37

maniqui
Member
From: Buenos Aires, Argentina
Registered: 2004-10-10
Posts: 3,070
Website

Re: Limit characters in body

Oh, sorry. My fault. That “body_html” may be just a table on the database, but not a proper function.

Are you using both snippets (the one for excerpt and the one for the body) on the same page? If so, may that be related because both uses the same $bod variable? I’m just guessing… again…


La música ideas portará y siempre continuará

TXP Builders – finely-crafted code, design and txp

Offline

#5 2009-11-02 16:22:29

jsoo
Plugin Author
From: NC, USA
Registered: 2004-11-15
Posts: 1,793
Website

Re: Limit characters in body

I’m not sure why the preg_replace() fails on body(), but at any rate it is much more efficient to use substr() since all you want is x characters from the start of the string.

echo substr(body(), 0, 100);

Code is topiary

Offline

#6 2009-11-02 16:38:29

beechy
Member
Registered: 2006-03-02
Posts: 92

Re: Limit characters in body

Hi jsoo,

If i use

<txp:php>
	$bod = strip_tags(body());
	$bod = preg_replace('/^(.{'.$limit.'}).*$/u', '$1', $bod);
	echo substr(body(), 0, 400);
</txp:php>

the body text shows up however it also strips the </p> tag Any ideas how I can get it to work so that it only limits the words

Last edited by beechy (2009-11-02 16:54:56)

Offline

#7 2009-11-02 18:45:32

jsoo
Plugin Author
From: NC, USA
Registered: 2004-11-15
Posts: 1,793
Website

Re: Limit characters in body

<txp:php>
	echo '<p>' . strip_tags(substr(body(), 0, 400)). '</p>';
</txp:php>

Just leave out the other stuff.

This isn’t very elegant, for one thing it just counts characters so will usually cut in the middle of a word.


Code is topiary

Offline

#8 2009-11-02 21:08:12

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,454
Website GitHub

Re: Limit characters in body

If you’re struggling to beat PHP into shape with tag stripping issues then ruud has done the hard work for you with rvm_substr (I know you originally said you weren’t going to use a plugin…)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#9 2009-11-02 22:39:44

jsoo
Plugin Author
From: NC, USA
Registered: 2004-11-15
Posts: 1,793
Website

Re: Limit characters in body

Bloke wrote:

If you’re struggling to beat PHP into shape with tag stripping issues then ruud has done the hard work for you with rvm_substr (I know you originally said you weren’t going to use a plugin…)

From the documentation:

This is a simple plugin to cut a string of plain text (not (X)HTML!)

Edit: Thanks Bloke; I wasn’t looking very carefully! Didn’t realize there were two substr plugins there…

Last edited by jsoo (2009-11-03 00:54:52)


Code is topiary

Offline

#10 2009-11-02 22:47:27

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,454
Website GitHub

Re: Limit characters in body

jsoo wrote:

This is a simple plugin to cut a string of plain text (not (X)HTML!)

Ahhh yes, but rvm_substr — ruud’s successor to sab_substr — does handle XHTML strings and, I believe, plays nicely with tags. Never used it in anger, but that’s what I’ve heard.

Last edited by Bloke (2009-11-02 22:47:38)


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#11 2009-11-03 07:51:43

the_ghost
Plugin Author
From: Minsk, The Republic of Belarus
Registered: 2007-07-26
Posts: 907
Website

Re: Limit characters in body

Why not to use rss_auto_excerpt?


Providing help in hacking ATM! Come to courses and don’t forget to bring us notebook and hammer! What for notebook? What a kind of hacker you are without notebok?

Offline

Board footer

Powered by FluxBB