Solved

PHP echo limit characters

Posted on 2009-07-05
6
1,227 Views
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);
	        ?>
          <tr>
            <td style="text-align: justify;">
            <?php echo($sModifyText); ?><br>
</td>
        <?php } while ($row_WADAnews = mysql_fetch_assoc($WADAnews)); ?>
      </table>

Open in new window

0
Comment
Question by:pda4me
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 4

Expert Comment

by:my2eggs
ID: 24779773
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

0
 
LVL 2

Expert Comment

by:Erbureth
ID: 24779792
echo( substr($sModifyText, 0, 15));
?

Or are you trying to achieve something else?
0
 
LVL 4

Expert Comment

by:stevepicks
ID: 24779834
i thinks he needs
$sText = substr($row_WADAnews['text'],0,25);
at line 3 of his code
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24779997
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
0
 

Author Comment

by:pda4me
ID: 24781272
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?
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 24785357
See if this helps.  Best regards, ~Ray
<?php // RAY_teaser_fragment.php
error_reporting(E_ALL);
 
// RETURN FIRST FEW WHOLE WORDS FOLLOWED BY ELLIPSES
// $length IS MINIMUM TRUNCATION CHARACTER COUNT
function teaser_fragment($text, $length=32, $delim='|||')
{
 
// IF NO TRUNCATION IS NEEDED
    if (strlen($text) <= $length)
    {
        return $text;
    }
 
// IF TRUNCATION IS NEEDED, BREAK APART AND ADD ELLIPSIS TO THE END
    $t = wordwrap($text, $length, $delim);
    $a = explode($delim, $t);
    $t = $a[0] . '...';
    return $t;
}
 
// USE CASES
echo "<pre>\n";
echo "1...5...10...15...20...25...30...35...40...45...\n";
echo teaser_fragment('Now is the time for all good men to come to the aid of their party');
 
echo "\n";
echo teaser_fragment('Now is the time for all good men to come to the aid of their party', 300);
 
echo "\n";
echo teaser_fragment('Now is the time for all good men to come to the aid of their party', 2);

Open in new window

0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question