Link to home
Start Free TrialLog in
Avatar of pda4me
pda4me

asked on

PHP echo limit characters

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);
	        ?>
          <tr>
            <td style="text-align: justify;">
            <?php echo($sModifyText); ?><br>
</td>
        <?php } while ($row_WADAnews = mysql_fetch_assoc($WADAnews)); ?>
      </table>

Open in new window

Avatar of my2eggs
my2eggs

The following function came from php.net.

http://php.net/manual/en/function.substr-replace.php

I believe there are other ways to do this other than creating your own function. I'll repost if I find it.
<?php
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
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
Avatar of pda4me

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial