Jump to content

Best way to limit character output of textarea field


Neo
 Share

Recommended Posts

I had a few situations were I needed to limit the output of a textarea-field to e.g. 150 characters and / or strip all html-tags to work with plain text.

I used the sanitizer-function like in the following example:

<?php // Echo the latest 3 items on homepage
foreach($pages->get("/services/")->children("limit=3, sort=-created") as $service){

	// sanitizer options
	$options = array(
        // maximum allowed characters for multibyte strings
              'maxLength' => 150,
	); 

	$service_preview = $sanitizer->textarea($service->body, $options);

	echo '<div class="one-third column">';
	echo '<article class="icon-box">';
	echo '<h3>'.$service->title.'</h3>';
	echo '<p>'.$service_preview.'... </p>';
	echo '</article>';
	echo '</div>';
} ?>

Is that the only way to limit character-output of a textarea?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...