Jquery查找纯文本形式的HTTP元素,并将其转换为链接


jquery find http elements in plain text and convert it to a link

我不知道这是否可能,但我有一段纯文本,其中总是包括:

http://www.royalmail/portal/etc/etc/track.php?trackNumber=123345323

当从数据库拖到我的页面时,这显然不呈现为链接。有没有办法选择这段文字(即通过http://),并在最基本的包裹锚标签-或更复杂-检索地址,包裹锚标签的地址,并改变原来的http://文本的东西更容易理解,如"跟踪包裹"

应该注意的是,可以同时循环出多个跟踪引用。

我的包含HTML如下

<div class="message_holder">
                <div class="mess_head" style="width:110px;">
                    <p><strong>Sender: </strong><? echo $row11['sender'];?></p>
                </div>
                <div class="mess_head" style="width:450px;">
                    <p><strong>Subject: </strong><span class="red"><? echo $row11['subject'];?></span></p>
                </div>
                <div class="mess_head" style="width:150px;">
                    <p><strong>Date Sent: </strong><? echo date('d/m/Y, H.ia', $row11['date_sent']);?></p>
                </div>
                <div class="mess_head" style="width:200px;">
                    <p><strong>Message ID: </strong><? echo $row11['message_id'];?></p>
                </div>
                <div class="mess_body">
                    <p><? echo html_entity_decode($row11['message']);?></p>
                </div>
            </div>

纯文本链接将全部显示在'mess_body'类中。我试过使用html_entity_decode(),但这并没有奏效。如果有一种简单的方法可以通过PHP而不是JQuery来实现,那就简单多了。

我会用PHP:

$text = preg_replace(
    '@(https?://([-'w'.]+)+(:'d+)?(/(['w/_'.]*('?'S+)?)?)?)@', 
    '<a href="$1">$1</a>', /* or <a href="$1">Your TEXT</a> */
    $text
);

如果我猜对了,url可以在你的消息中,所以你的代码应该像这样:

<?php 
    echo 
     preg_replace(
        '@(https?://([-'w'.]+)+(:'d+)?(/(['w/_'.]*('?'S+)?)?)?)@smi', 
        '<a href="$1">$1</a>',
        html_entity_decode($row11['message'])
     );
 ?>