We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


PHP echo limit characters

pda4me asked
Medium Priority
Last Modified: 2013-12-13
How do I adjust the results of this echo statement to limit the output to the first 25 characters found in the record?
<table class="WADAResultsTable" border="0" cellpadding="0" cellspacing="0">
        <?php do { 
	        $sText = $row_WADAnews['text'];
	        $sModifyText = str_replace("<img ","<img width='90' height='113' ",$sText);
            <td style="text-align: justify;">
            <?php echo($sModifyText); ?><br>
        <?php } while ($row_WADAnews = mysql_fetch_assoc($WADAnews)); ?>

Open in new window

Watch Question

The following function came from php.net.


I believe there are other ways to do this other than creating your own function. I'll repost if I find it.
function truncate($string, $max = 20, $replacement = '')
    if (strlen($string) <= $max)
        return $string;
    $leave = $max - strlen ($replacement);
    return substr_replace($string, $replacement, $leave);

Open in new window

echo( substr($sModifyText, 0, 15));

Or are you trying to achieve something else?
i thinks he needs
$sText = substr($row_WADAnews['text'],0,25);
at line 3 of his code
Most Valuable Expert 2011
Author of the Year 2014

You can truncate the string as shown by my2eggs and stevepicks, or you can break it apart on a word boundary if you are willing to write a little more code, but the question appears to be illogical, and here is why.

On line 3 you get a variable called $sText out of a data base row.
On line 4, you replace 4 characters of this field with a string that is 30 characters long.

Thus you have added 26 characters to the contents of this variable, which makes "limit the output to the first 25 characters" something that is hard to grok.

Can you please explain what you are trying to achieve?  If we understand the inputs and the desired outputs we may be able to offer more productive suggestions.  Thanks, ~Ray


I was looking for along the lines of stevepicks solution but it does not appear to work...is there a syntax issue?  Good point Ray, the 25 limit I specified was just a number I planned on changing once I see how the data displays...I am shooting for more along the lines of 175 characters.  Any suggestions?
Most Valuable Expert 2011
Author of the Year 2014
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.