Limiting the Output of PHP's Echo to 200 Characters

Limiting the output of PHP's echo to 200 characters

Well, you could make a custom function:

function custom_echo($x, $length)
{
if(strlen($x)<=$length)
{
echo $x;
}
else
{
$y=substr($x,0,$length) . '...';
echo $y;
}
}

You use it like this:

<?php custom_echo($row['style-info'], 200); ?>

How do i limit characters echoed from database?

PHP's substr() is what you're looking for: http://www.php.net/manual/en/function.substr.php

<?php echo substr($row_posts['tekst'], 0, 20); ?>

Limit Displayed Character PHP

TRY

<?php
$articlesql = "SELECT articles.*, categories.category_name, admin.admin_name FROM articles, categories,
admin WHERE articles.article_category_id=categories.category_id AND articles.article_admin_id=admin.admin_id";
$articleresult = mysql_query($articlesql);
while($articlerow=mysql_fetch_assoc($articleresult)){

$string = strip_tags($articlerow['article_content']);
if (strlen($string) > 500) {
// truncate string
$stringCut = substr($string, 0, 500);
// make sure it ends in a word so assassinate doesn't become ass...
$string = substr($stringCut, 0, strrpos($stringCut, ' '))."... <a href='article-single.php?id=".$articlerow['article_id']."' title='view details'>Read More</a>";
}

echo "
<li>
<span></span>
<div>
<h4><a href='article-single.php?id=".$articlerow['article_id']."' title='view details'>".$articlerow['article_title']."</a></h4>
<span>
Posted in <a href = '#'>".$articlerow['category_name']."</a> by <a href='#'>".$articlerow['admin_name']."</a> on <a href='#'>".$articlerow['article_date']."</a>
</span>
</div>
<a href='article-single.php?id=".$articlerow['article_id']."' title='view details'><img src='images/blog-post-1.jpg' alt=''></a>
<p>".$string."</p>
</li>
";
}

Limit String Length

You can use something similar to the below:

if (strlen($str) > 10)
$str = substr($str, 0, 7) . '...';

limit text length in php and provide 'Read more' link

This is what I use:

// strip tags to avoid breaking any html
$string = strip_tags($string);
if (strlen($string) > 500) {

// truncate string
$stringCut = substr($string, 0, 500);
$endPoint = strrpos($stringCut, ' ');

//if the string doesn't contain any space then it will cut without word basis.
$string = $endPoint? substr($stringCut, 0, $endPoint) : substr($stringCut, 0);
$string .= '... <a href="/this/story">Read More</a>';
}
echo $string;

You can tweak it further but it gets the job done in production.

How can I truncate a string to the first 20 words in PHP?

function limit_text($text, $limit) {
if (str_word_count($text, 0) > $limit) {
$words = str_word_count($text, 2);
$pos = array_keys($words);
$text = substr($text, 0, $pos[$limit]) . '...';
}
return $text;
}

echo limit_text('Hello here is a long sentence that will be truncated by the', 5);

Outputs:

Hello here is a long ...

PHP/MySQL Limiting characters outputted from a field

You can use substr like this:

<?php echo substr($row['description'], 0, 100); ?>

But it might be better—depending on your application needs—to do the limiting when making the initial MySQL query using SUBSTR which behaves the same way, but in MySQL.

SELECT SUBSTR(example_field, 1, 100)
FROM example_table
WHERE example_field IS NOT NULL
LIMIT 1
;

That MySQL script basically means return the substring of example_field starting from the first (1) character and going 100 characters in.

This might be better in some cases since if you are limiting text length to 100 characters, why grab the data for fields that might have 1,000+ characters? That would definitely bog down your PHP script in many cases. Handling that in MySQL is the best way to nip it in the bud if your app just needs 100 characters returned.

Get first 100 characters from string, respecting full words

All you need to do is use:

$pos=strpos($content, ' ', 200);
substr($content,0,$pos );

How To Set character_limiter() in Codeigniter

http://ellislab.com/codeigniter/user-guide/helpers/text_helper.html

You should import Text Helper

in your controller, it is a good practice to load helper, models and libraries in a constructor

function __construct()
{
parent::__construct();
$this->load->helper('text');
$this->load->model('products_model'); //name of your model class

}

function index()
{
$data['products']=$this->products_model->selectAllProduct();
$this->load->view('index',$data);
}

at your view index.php

//This is an example from CI's home page        
//$string = "Here is a nice text string consisting of eleven words.";
//$string = word_limiter($string, 4);

foreach($products as $p)
{
$limited_word = word_limiter($p[product_title],25);
echo $limited_word;
}


Related Topics



Leave a reply



Submit