Simple question for you's......

S

Slow Learner

Hey gurus,


If, say, I have a URL (or any "path" type string come to think of it), how
can I truncate it if it's greater than a certain length? Note: I don't want
to use it as a path, I just want to truncate it from display purposes. For
example:


http://www.mysite.com/beta/content/english/misc/publications/papers/index.html


..... and I want to restrict it to no more than 50 characters, but I want the
result to look something like this:


..../english/misc/publications/papers/index.html


In other words, I want the left-most part of a path string replaced with
".../" for those bits of the path that make it go over 50 characters.



How can I do this in Perl?


Thanks
 
J

Josef Moellers

Slow said:
Hey gurus,


If, say, I have a URL (or any "path" type string come to think of it), how
can I truncate it if it's greater than a certain length? Note: I don'twant
to use it as a path, I just want to truncate it from display purposes. For
example:


http://www.mysite.com/beta/content/english/misc/publications/papers/index.html


.... and I want to restrict it to no more than 50 characters, but I want the
result to look something like this:


.../english/misc/publications/papers/index.html


In other words, I want the left-most part of a path string replaced with
".../" for those bits of the path that make it go over 50 characters.



How can I do this in Perl?

The usual answer: What have you tried so far and where have you failed?

I'd try split and length, but then TMTOWTDI,

Josef
 
A

Anno Siegel

Slow Learner said:
Hey gurus,


If, say, I have a URL (or any "path" type string come to think of it), how
can I truncate it if it's greater than a certain length? Note: I don't want
to use it as a path, I just want to truncate it from display purposes. For
example:


http://www.mysite.com/beta/content/english/misc/publications/papers/index.html


.... and I want to restrict it to no more than 50 characters, but I want the
result to look something like this:


.../english/misc/publications/papers/index.html


In other words, I want the left-most part of a path string replaced with
".../" for those bits of the path that make it go over 50 characters.



How can I do this in Perl?

Lots of ways, for sure.

You can split it up in parts, then put them together again, watching
the length. Prepend three dots if anything is missing from the original:

my $str = 'http://www.mysite.com/beta/content/english/misc/' .
'publications/papers/index.html';
my $max = 50;

my ( $trunc, @parts) = reverse split m{/}, $str;
for ( @parts ) {
last if length( $trunc) + length >= $max - 4; # four dots and a slash
$trunc = "$_/$trunc";
}
$trunc = ".../$trunc" unless $trunc eq $str;

print "$trunc\n";
print length( $trunc), "\n";

Anno
 
S

Slow Learner

As I'm not from a Perl background, I would probably try something like:

if ( length ( $display_url ) > 70 ) {
$display_name = '.../'.substr ($display_url, length (
$display_url ) - 70, 70 );
}
else
{
$display_name = $display_url;
}


This is my naive version - sorry I don't know Perl syntax all that well.
I'm currently modifying someone elses script to make this small change :/.
Anyway, the above kindof works (okay, given 70 chars instead of 50), but
with urls that happen to straddle the http:// on 70 characters, it looks
rather ugly, ie:

..../://www.mysite.com/bla/bla/bla





Slow said:
Hey gurus,


If, say, I have a URL (or any "path" type string come to think of it), how
can I truncate it if it's greater than a certain length? Note: I don't
want to use it as a path, I just want to truncate it from display
purposes. For example:


http://www.mysite.com/beta/content/english/misc/publications/papers/index.html


.... and I want to restrict it to no more than 50 characters, but I want
the result to look something like this:


.../english/misc/publications/papers/index.html


In other words, I want the left-most part of a path string replaced with
".../" for those bits of the path that make it go over 50 characters.



How can I do this in Perl?

The usual answer: What have you tried so far and where have you failed?

I'd try split and length, but then TMTOWTDI,

Josef
 
B

Brian McCauley

Slow said:
Subject: Simple question for you's......

Please put the subject of your post in the Subject of your post.
http://www.mysite.com/beta/content/english/misc/publications/papers/index.html

.... and I want to restrict it to no more than 50 characters, but I want the
result to look something like this:

.../english/misc/publications/papers/index.html
In other words, I want the left-most part of a path string replaced with
".../" for those bits of the path that make it go over 50 characters.

s/.+?(?=\/.{0,46}$)/.../ if length > 50;

Note: This will leave the string unchaged if there is no '/' in the last
47 characters of the string.
 
W

William James

Brian said:
Slow Learner wrote:


s/.+?(?=\/.{0,46}$)/.../ if length > 50;

Note: This will leave the string unchaged if there is no '/' in the last
47 characters of the string.

A very cleverly constructed regular expression. It's fascinating how
the combination of a non-greedy part and a greedy one solves the
problem.
 
S

Shawn

Brian said:
Please put the subject of your post in the Subject of your post.



s/.+?(?=\/.{0,46}$)/.../ if length > 50;

Note: This will leave the string unchaged if there is no '/' in the last
47 characters of the string.

Or, similarly

s/^.*?(.{47})$/...\1/ if length > 50;


- Shawn
 
J

Josef Moellers

Slow said:
As I'm not from a Perl background, I would probably try something like:

if ( length ( $display_url ) > 70 ) {
$display_name = '.../'.substr ($display_url, length (
$display_url ) - 70, 70 );
}
else
{
$display_name = $display_url;
}


This is my naive version - sorry I don't know Perl syntax all that well.
I'm currently modifying someone elses script to make this small change :/.
Anyway, the above kindof works (okay, given 70 chars instead of 50), but
with urls that happen to straddle the http:// on 70 characters, it looks
rather ugly, ie:

.../://www.mysite.com/bla/bla/bla








The usual answer: What have you tried so far and where have you failed?

I'd try split and length, but then TMTOWTDI,

See Anno's post. I'd have done something similar, although not as
elegant B-{).

Josef
 
W

William James

Shawn said:
Or, similarly

s/^.*?(.{47})$/...\1/ if length > 50;


- Shawn

Unlike the previous regexp, this doesn't always perform the cut just
before a / .
 
W

William James

Slow said:
Hmmmmm interesting. Gibberish but interesting....... :)

It's not gibberish if you understand regular expressions. You should
learn regular expressions if you want to program. Don't be discouraged
by their apparent complexity; it's easier to write one than to read
one.

..+ # Match 1 or more characters.
? # Make the preceding expression "non-greedy".
(?= # Begin a sequence that must be satisfied but that won't "consume"
# any characters (zero-width look-ahead).
\/ # Match a forward slash.
..{0,46} # Match any character from 0 to 46 times "greedily".
$ # Match the end of the string.
)
 
B

Brian McCauley

Anno said:
Very nice. I believe the final "$" can go, the last part is greedy
anyhow.

No, that would match upto the first '/' anywhere in the string. The OP
wants to match upto first '/' within the last 47 characters of the string.
 
S

Slow Learner

I kind-of understand them, however the concept of greediness in an
expression has passed me by somewhere along the line. I think it means
"consuming" the character, ie: it isn't available in a subsequent part of
the expression, but don't quote me :)))).

But, if it works, I'll use it and I'm grateful, so thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top