Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A feature on this Forum

Status
Not open for further replies.

blitzer

Programmer
Jun 18, 2001
34
0
0
CA
I'm pretty sure that when I type in , it will not only show up in my message on this Forum, but it will do so as a link.

Right now one of the scripts I use has a variable which is taken from a database with text and sometimes links ( $text_var ). I want $text_var to be read and have links converted into.. links.

I'd like to aviod using the split command if there's a way, but if you have an idea using the split command I'd still like to hear it.

I've tried this:
$text_var =~ s/ http:\/\/(.*) / <a href=\&quot;http:\/\/$1\&quot;>http:\/\/$1</a> /g;

The problem is, the (.*) command reads all information between http:// and the last space ( ) of $text_var. I end up getting something like this when $text_var is printed:

This is a good site here: <a href=&quot; it is just the best site ever. visit it sometime&quot;> it is just the best site ever. visit it sometime</a>

Obviously, I want it just to read between http:// and the next space of $text_var

Thanks.
 
maybe this is what you are trying to do...

$text_var =~ s/ http:\/\/(.*?)\s+/<a href=\&quot;http:\/\/$1\&quot;>http:\/\/$1</a>/g;

note the addition of the '?' in the parens. The '?' forces a minimal match - or - match up to the first instance of the following item in the match pattern. Also, not the '\s+' to match any number of white space chars. This is a little more general that a space, in case there are multiple spaces or tabs or other.....


HTH


keep the rudder amid ship and beware the odd typo
 
If I may be so bold, there's a small error in that line..

$text_var =~ s/ http:\/\/(.*?)\s+/<a href=\&quot;http:\/\/$1\&quot;>http:\/\/$1</a>/g;

should be:

$text_var =~ s/ http:\/\/(.*?)\s+/<a href=\&quot;http:\/\/$1\&quot;>http:\/\/$1<\/a>/g;

Please note the backslash inside the
Code:
</a>
tag at the end of the line. A small mistake, but it gave all kinds of errors for me.

Hope that helps,
Segfault
 
This is a classic example of LTS (Leaning Toothpick Syndrome). There's a very easy way to avoid having to escape all those slashes in a regex: Don't use slashes as the regex delimiter! Perl allows you to use almost any character for the delimiter, as long as you specify the s or m in front of the regex. You can even use paired delimiters like (), {}, <>, and my favorite, []. Here's what that statement would look like without LTS:
Code:
$text_var =~ s[ [URL unfurl="true"]http://(.*?)\s+[/URL]][<a href=\&quot;[URL unfurl="true"]http://$1\&quot;>http://$1</a>[/URL]]g;
Lots easier to read, isn't it?
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top