Java检测url

发布于 2024-12-13 23:11:42 字数 1016 浏览 4 评论 0原文

我正在尝试用java制作它,这样如果我输入一条包含链接的消息,它会自动将其格式化为html,以便可以在网页上单击:P

但是,我编写的代码仅打开第一个“链接”在我的消息中指向一个链接,而不是其他链接。

有人可以帮我解决这个问题吗?我没有主意了......

我的代码

// 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];
}

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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我ぃ本無心為│何有愛 2024-12-20 23:11:42

使用 replaceAll() 而不是 replace()

编辑:

您可以使用像这样的正则表达式来更简单、更干净,而不是使用 split :

msg.replaceAll("http://[^ ]+", "<a href=\"$0\">$0</a>");

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>");
逆蝶 2024-12-20 23:11:42

对于附加图像,您可以使用两次替换(第二次替换为负向后查找:

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);

为您提供:

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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文