0

I'm trying to make it in java so that if I type a message that contains a link it automaticly formats it with html so it can be clickable on a webpage :P

However, the code i have written only turns the first "link" in my message to a link, not the others.

Can someone help me with this? I'm out of ideas...

My Code

// URL and Image handling
    if (msg.contains("http://")) {
        // If url is an image, embed it
        if (msg.contains(".jpg") || msg.contains(".png") || msg.contains(".gif")) {
            msg = msg.replace(linkz(msg, true), "<img src='" + linkz(msg, true) + "' class='embedded-image' />");
        }
        // Send link as link in <a> tag
        msg = msg.replace(linkz(msg, true), "<a href='" + linkz(msg, true) + "' class='msg-link' target='_blank' title='" + linkz(msg, false) + "'>" + linkz(msg, false) + "</a>");
    }

// Check string for links and return the link
public static String linkz(String msg, boolean http) {
    String[] args = msg.split("http://");
    String[] arg = args[1].split(" ");
    if (http == true) {
        return "http://" + arg[0];
    }
    return arg[0];
}
1
  • Also, keep in mind not all URLs start with "http://" (unless you are the only one adding content and can guarantee you will always remember the http://). you can find some pretty nice regular expressions with some googling, though I've never found a perfect one. Commented Nov 4, 2011 at 13:40

2 Answers 2

1

Use replaceAll() instead of replace().

EDIT :

You can do it way simpler and cleaner with regex like this, instead of using splits :

msg.replaceAll("http://[^ ]+", "<a href=\"$0\">$0</a>");
Sign up to request clarification or add additional context in comments.

3 Comments

Ye ty! But now if I post an image link and a regular link, the firest one changes the other... If I post a image link and then a link both turns to images.... My current code: pastebin.com/xsiPiuAM
getLinks() is in the class named CheckLinks.class and check() is in Handler.class
You can add somehting like '(png|jpg|etc...)' at the end of the regex to check if the link corresponds to a picture or not.
0

For the additional images, you could use two replaces (with a negative look-behind for the second replace:

String msg = 
    "this is an example https://test.com/img.jpg " +
    "for http://www.test.com/ and yet more " +
    "http://test/test/1/2/3.img.gif test and more " +
    "https://www.test.com/index.html";

// replace images with img tag 
msg = msg.replaceAll(
    "https?://[^ ]+\\.(gif|jpg|png)", 
    "<img src=\"$0\" class=\"embedded-image\" />");

msg = msg.replaceAll("(?<!img src=\")https?://([^ ]+)", 
    "<a href=\"$0\" class=\"msg-link\" target=\"_blank\" title=\"$1\">$1</a>");

System.out.println(msg);

Gives you:

this is an example <img src="https://test.com/img.jpg" class="embedded-image" /> 
for <a href="http://www.test.com/" class="msg-link" target="_blank" 
title="www.test.com/">www.test.com/</a> and yet more 
<img src="http://test/test/1/2/3.img.gif" class="embedded-image" /> 
test and more <a href="https://www.test.com/index.html" class="msg-link" 
target="_blank" title="www.test.com/index.html">www.test.com/index.html</a>

3 Comments

Would it maybe be able to do this in javascript instead? :P Will it be the same code?
and if I use this. how would implement my youtube detetion code?
@enjikaka: Javascript does not implement negative lookbehind (the (?<!img src=\") bit, so it would be a bit different. Though I'd say that's another question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.