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
PHP SQL, Query Returns Only One Row of Data
Protection Against Xss Exploits
Rest API Authorization & Authentication (Web + Mobile)
PHP Composer Update "Cannot Allocate Memory" Error (Using Laravel 4)
PHP Array Find Duplicates, Sum Them Up & Delete Duplicates
Post to a Facebook Page Without "Manage_Pages" Permission Using PHP
How Best to Get Someone's 'Rank' from a Scores Table with PHP and MySQL Without Looping
What Is Null Coalescing Assignment = Operator in PHP 7.4
Overriding Upload_Max_Filesize
How to Create a Base64Encoded String from Image Resource
Split Array into Two Arrays by Index Even or Odd
Can't Install PHPunit via Pear, Requires Pear Installer >= 1.9.2, Can't Upgrade Pear from 1.9.0
Get the First or Last Friday in a Month
Compile Error: Cannot Use Isset() on the Result of an Expression
Downloading a File with a Different Name to the Stored Name
Dynamic Paypal Button Generation - Isn't It Very Insecure
MySQL Get a Random Value Between Two Values
Loadhtml Libxml_Html_Noimplied on an HTML Fragment Generates Incorrect Tags