Help - Search - Members - Calendar
Full Version: Regexp for converting links
Movable Type Community Forum > Additional Resources > Tips and Tricks
craig1972
Hi, I want to automatically convert links in my comments into clickable hyperlinks, for which I use Brad CHoate's MTMacro plugin. Here is what I have in my code, I guess I am missing something!

CODE
<MTMacroDefine name="convertlinks" pattern="m/(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)/"><a href="\\1">\\1</a></MTMacroDefine>


When I rebuild with this, I get the following error:

CODE
Building entry 'HELLO WORLD' failed: Build error in template 'Individual Entry Archive': Error in <MTEntryIfAllowComments> tag: Error in macro pattern: Unmatched ( in regex; marked by <-- HERE in m/(( <-- HERE (f|ht){1}tp:/ at (eval 88) line 2, <DATA> line 252.


Is my pattern definition wrong? It is valid code from PHP!

Thanks.
markpasc
You wrote:

m/(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)/

Try writing:

m/(((f|ht){1}tp:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/

That first slash in http:// is the end of the regex as far as the plugin's concerned; that's why it only lists through there in the error message.
craig1972
Excellen, thanks! I also escaped the other characters except for numbers and characters and it works! smile.gif
clio
I tried the following:
CODE
<MTMacroDefine name="convertlinks" pattern="m/(((f|ht){1}tp:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/
"><a href="\\1">\\1</a></MTMacroDefine>


which doesn't work. What am I doing wrong?
clio
I tried this:

CODE
<MTMacroDefine name="convertlinks" pattern="m/(((f|ht){1}tp:\/\/)[-a-zA-Z0-9\@:%_\+.~#?&\/\/=]+)/"><a href="\\1">\\1</a></MTMacroDefine>


and it turns all my links into gibberish.

CODE
<p><a href="\\1">\\1</a><br />
<a href="\\1">\\1</a><br />
<a href="\\1">\\1</a><br />
<a href="\\1">\\1</a></p>


That's what ALL my links turned into. Sorry, I'm a little slow....
clio
Some progress...

CODE
<MTMacroDefine name="convertlinks" pattern="m/(((f|ht){1}tp:\/\/)[-a-zA-Z0-9\@:%_\+.~#?&\/\/=]+)/"><a href="<MTMacroMatch>"><MTMacroMatch></a></MTMacroDefine>


Anybody?
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.