Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
#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
Re: Limit characters in body
Try using body_html instead of body.
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
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…
Offline
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
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
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
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
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
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
Pages: 1