CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
Limiting text


Warren Bell

Oct 12, 2000, 11:20 AM

Post #1 of 4 (667 views)
Limiting text Can't Post

I want to limit some text to be so many sharactors long no matter how long the string is. The string will contain anything typed into a textarea field plus html tags. This is what I'm using:

$text =~ s/(.{200}).*/$1\.\.\./;

The problem is sometimes it breaks off in the middle of and html tag and that messes up the page. Is there a way I can have it break off at 200 charactors but ignore the html tags so it's counting everything but the tags?


Oct 13, 2000, 6:50 AM

Post #2 of 4 (667 views)
Re: Limiting text [In reply to] Can't Post

You could try this...

$text = substr($text,0,200);

That would give you the first 200 char only. Im not sure if that is what you are looking for but that would work.

Perl...Love it and Live it.


Oct 13, 2000, 11:44 AM

Post #3 of 4 (667 views)
Re: Limiting text [In reply to] Can't Post

The approach you're taking risks leaving partial tag pairs in the truncated string. For example, one truncated string could contain an unpaired font change. That might screw up later formatting. Are you sure that something you're O.K. with?

You might try something like the following, (which I'm typing in off the top of my head without testing):
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

$truncated = "";
if ( length($text) <= 200 ) {
$truncated = $text;
else {
while ( $text =~ m/^(<.*?>|[^<]*)/g ) {
$truncated .= $1;
last if length($truncated) >= 200;
$truncated .= "...";
This assumes that $text holds well-behaved HTML.

[This message has been edited by dws (edited 10-13-2000).]

Warren Bell

Oct 13, 2000, 11:58 AM

Post #4 of 4 (667 views)
Re: Limiting text [In reply to] Can't Post

Ok, I'll try that thanks.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives