簡體   English   中英

在 PHP 中將純文本 URL 轉換為 HTML 超鏈接

[英]convert plain text URLs to HTML hyperlinks in PHP

有沒有辦法在 PHP 中將純文本 URL 轉換為 HTML 超鏈接? 我需要轉換以下類型的 URL:

http://example.com
http://example.org
http://example.gov/
http://example.com?q=sometext&opt=thisandthat
http://example.com#path
http://example.com?q=querypath#path
http://example.com/somepath/index.html

https://example.com/
https://example.org/
https://example.gov/
https://example.com?q=sometext&opt=thisandthat
https://example.com#path
https://example.com?q=querypath#path
https://example.com/somepath/index.html

http://www.example.com/
http://www.example.org/
http://www.example.gov/
http://www.example.com?q=sometext&opt=thisandthat
http://www.example.com#path
http://www.example.com?q=querypath#path
http://www.example.com/somepath/index.html

https://www.example.com/
https://www.example.org/
https://www.example.gov/
https://www.example.com?q=sometext&opt=thisandthat
https://www.example.com#path
https://www.example.com?q=querypath#path
https://www.example.com/somepath/index.html

www.example.com/
www.example.org/
www.example.gov/
www.example.com?q=sometext&opt=thisandthat
www.example.com/#path
www.example.com?q=querypath#path
www.example.com/somepath/index.html

example.com/
example.org/
example.gov/
example.com?q=sometext&opt=thisandthat
example.com/#path
example.com?q=querypath#path
example.com/somepath/index.html

如何通過如下用於重新激活 Twitter 鏈接的正則表達式來運行它們 -

http://www.burnmind.com/howto/how-to-convert-twitter-mentions-and-urls-to-active-links-using-regular-expressions-in-php

如果您只需要轉換單個 URL,那非常簡單:

function makeLink($url)
{
    return '<a href="' . htmlspecialchars($url) . '">' . htmlspecialchars($url) . '</a>';
}

對於出現在較大文本塊中的 URL,例如用戶評論,請參閱以下問題:

試試這個功能。 如果文本以 http 或 www 開頭,這將建立鏈接。 example.com 將不起作用。

function linkable($text = ''){
    $text = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $text));
    $data = '';
    foreach( explode(' ', $text) as $str){
        if (preg_match('#^http?#i', trim($str)) || preg_match('#^www.?#i', trim($str))) {
            $data .= '<a href="'.$str.'">'.$str.'</a> ';
        } else {`enter code here`
            $data .= $str .' ';
        }
    }
    return trim($data);
}

呃,將它們包裹在錨標簽中?

function linkify($url) {
  return '<a href="' . $url . '">' . $url . '</a>';
}

這是我的做法(包括測試用例):

/**
 * @see Inspired by https://css-tricks.com/snippets/php/find-urls-in-text-make-links/ and Example 1 of http://php.net/manual/en/function.preg-replace-callback.php
 * 
 * @param string $text
 * @return string
 */
public static function getTextWithLinksEnabled($text) {
    $regex = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
    return preg_replace_callback($regex, function ($matches) {
        $fullMatch = $matches[0];
        return "<a href='" . $fullMatch . "'>" . $fullMatch . "</a>";
    }, $text);
}

測試:

public function testGetTextWithLinksEnabled() {
    $this->assertEquals('a@b.com', StrT::getTextWithLinksEnabled('a@b.com'));//no links
    $this->assertEquals('just plain text', StrT::getTextWithLinksEnabled('just plain text'));//no links
    $this->assertEquals("here is a link <a href='https://stackoverflow.com/'>https://stackoverflow.com/</a> right here", StrT::getTextWithLinksEnabled('here is a link https://stackoverflow.com/ right here'));//one link
    $this->assertEquals("here is a link <a href='https://stackoverflow.com/'>https://stackoverflow.com/</a> right here and another: <a href='https://css-tricks.com/snippets/php/find-urls-in-text-make-links/'>https://css-tricks.com/snippets/php/find-urls-in-text-make-links/</a>", StrT::getTextWithLinksEnabled('here is a link https://stackoverflow.com/ right here and another: https://css-tricks.com/snippets/php/find-urls-in-text-make-links/'));//2 links
}

這是我在我做的一個小論壇上用來做同樣事情的。 通常我會像echo makeLinks($forumpost['comment']);一樣運行整個評論。

function makeLinks($str) {    
    return preg_replace('/(https?):\/\/([A-Za-z0-9\._\-\/\?=&;%,]+)/i', '<a href="$1://$2" target="_blank">$1://$2</a>', $str);
}

如果最終用戶不包含“http://”或“https://”(在我的例子中是將鏈接復制到表單中)。 基於我上面 Ryan 的代碼:

function makeLink($text) {
    $regex = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
    return preg_replace_callback($regex, function ($matches) {
        $fullMatch = $matches[0];
        // $fullMatch = startsWith($fullMatch, 'http') ? $fullMatch : 'http://'.$fullMatch;
        return "<a href='" . $fullMatch . "'>" . $fullMatch . "</a>";
    }, substr( $text, 0, strlen( 'http' ) ) === 'http' ? $text : 'http://'.$text);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM