Gossamer Forum
Home : General : Perl Programming :

Chop chop ...

Quote Reply
Chop chop ...
I have a text string that contains, say, 600 characters. Now I want to cut it off at, say, 500 characters, and add a " ...". (In fact, the string must be max. 500 characters, so perhaps I need to cut it of at 496 to make room for the " ..." ?)

I will use it in a subroutine that also removes HTML tags, which I found out can be done like this:

foreach $lines (@lines) {
$lines =~ s/<([^>]|\n)*>//g;
}

Anyone? Thanks in advance.

John


Quote Reply
Re: Chop chop ... In reply to
try this
$truncate = substr($truncate,0,496);
if (length($truncate) > 495) {
$truncate =~ s/\s+\S*$/.../;
}

Quote Reply
Re: Chop chop ... In reply to
Thanks Bmxer,

Hmmm ... I am not sure I understand how this goes with the code I already have:

foreach $lines (@lines) {
$lines =~ s/<([^>]|\n)*>//g;
}
$lines = substr($lines,0,496);
if (length($lines) > 495) {
$lines =~ s/\s+\S*$/.../;
}

does not seem to work. And if I use $truncate as you suggested, how does this "communicate" with $lines?

(Oh, do I need to say that I'm am a real novice ...?)

Quote Reply
Re: Chop chop ... In reply to
Code:
foreach $lines (@lines) {
$lines =~ s/<([^>]|\n)*>//g;
$lines = substr($lines,0,496);
if (length($lines) > 495) {
$lines =~ s/\s+\S*$/.../;
}
}
JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: Chop chop ... In reply to
Thanks Carol!

Now I got it Smile

John