0

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

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 做到这一点,它可能会更简单。

4

1 回答 1

3

我会用 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'])
     );
 ?>
于 2013-01-13T09:56:39.247 回答