使用PHP将纯文本URL转换为活动链接 [英] Turn Plain Text URLs into Active Links using PHP

查看:91
本文介绍了使用PHP将纯文本URL转换为活动链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手。我想知道该怎么做,因为我一直想和我的朋友开个玩笑。你能回答吗?感谢!

解决方案

您可能想知道它是如何工作的。我会试着解释如何通过各种方法来完成。正则表达式正则表达式正则表达式




在计算中,正则表达式(缩写为regex或regexp)是形成搜索模式的
字符序列,主要用于使用
模式匹配字符串或字符串匹配,即查找和
替换类似的操作。

基本语法



要首先使用正则表达式,您需要学习语法。此语法由一系列字母,数字,点,连字符和特殊符号组成,我们可以使用不同的括号将它们组合在一起。

  ^符号符号与输入字符串或行的开头相匹配,但在某些情况下可省略
$与回音符号相同,美元符号与输入字符串或行$ b $的末尾匹配b。该期间是否与任何单个字符
匹配?它将匹配前面的模式零次或一次
+它将匹配前面的模式一次或多次
*它将匹配前面的模式零次或多次
| Boolean OR
- 描述元素范围时使用
()将模式元素组合在一起
[]匹配方括号之间的任何单个字符
{min,max}用于匹配精确的字符计数,其中min和max是整数
\d匹配任何单个数字
\D匹配任何单个非数字字符
\w匹配任何字母数字字符,包括下划线(_ )
\W匹配任何非字母数字字符,不包括下划线字符
\s匹配任何单个空格字符

括号



括号 [] 在正则表达式的上下文中使用时有特殊含义。

  [0-9]匹配从0到9的任何十进制数字。
[az]匹配从小写字母a到小写字母z的任何字符。
[AZ]匹配从大写字母A到大写字母Z的任何字符。
[aZ]匹配从小写字母a到大写字母Z的任何字符。

示例



让我们看看如何正确使用操作符。我们将以 hello 这个单词为例来说明。

  / hello /匹配单词hello 
/ ^ hello /匹配字符串开头的hello。可能的匹配是hello或helloworld,但不是worldhello
/ hello $ /匹配字符串或行尾的hello。
/he.o/匹配他和o之间的任何角色。可能的匹配是helo或heyo,但不是hello
/ he?llo /匹配hllo或hello
/ hello + /匹配hello一次或多次。例如。匹配hello或hellohello
/ he * llo /匹配llo,hello或hehello,但不是hellooo
/ hello | world /匹配hello或world
/(AZ)/使用连字符表示范围,匹配A到ZEg中的每个大写字符A,B,C ...
/ [abc] /匹配任何单个字符a,b或c
/ abc {1} /匹配字符ab后的一个c字符。例如。匹配abc,但不是abcc
/ abc {1,} /在字符ab后匹配一个或多个c字符。例如。匹配abc或abcc
/ abc {2,4} /匹配字符ab后的两个和四个c字符之间的匹配。例如。匹配abcc,abccc或abcccc,但不是abc

最常见的

  [^ a-zA-Z]匹配任何不包含范围从a到z和A到Z的字符的字符串。 
pp匹配任何包含p的字符串,后跟任何字符,接着是另一个p。
^。{2} $匹配任何包含两个字符的字符串。
< b>(。*)< / b>匹配< b>中的任何字符串和< / b> ;.
p(hp)*匹配任何包含p的字符串,后跟零个或多个序列hp的实例。






正则表达式匹配URL



首先让我们看看如何构建一个URL。我们只有几个选项:




  • http://example.com/

  • https://example.com/

  • ftp://example.com/

  • www.example.com

  • user@example.com

  • 127.0.0.1

  • http://example.com:8080/



http:// https:// ftp , www 邮件 ip p>

  //仅邮件
$ match = preg_match('/ [^ \x00-\x20()< > @;:\\ [\] \x7f-\xff] +(?: \ [^ \x00-\x20()<> @,;: \\ [\] \x7f-\xff] +)* \ @ [^ \x00-\x20()<> @;:\\。[ \] \x7f-\xff] +(?: \ [^ \x00-\x20()<> @;:。\\ [\] \x7f -\X ff] +)+ /',$ string,$ array);

方法2 (5/10分)

  //没有端口,www-s,ip -s和邮件
$ text = ereg_replace([[:alpha:]] + :// [^<> [:space:]] + [[:alnum:] /],< a href = \\\\> \\ <0 ; / a>,$ text);

方法3 (10/10分)

  / *建议者:
*SørenLøvborg
* http://stackoverflow.com/users/136796/soren-lovborg
* /

$ rexProtocol ='(https?://)?';
$ rexDomain ='((?:[ - a-zA-Z0-9] {1,63} \。)+ [ - a-zA-Z0-9] {2,63} |(? :[0-9] {1,3} \){3} [0-9] {1,3})';
$ rexPort ='(:[0-9] {1,5})?';
$ rexPath ='(/ [!$ - / 0-9:; = @ _ \':;!a-zA-Z\x7f-\xff] *?)?';
$ rexQuery ='(\?[!$ - / 0-9:; = @ _ \':;!a-zA-Z\x7f-\xff] +?)?';
$ rexFragment ='(#[!$ - / 0-9:; = @ _ \':;!a-zA-Z\x7f-\xff] +?)?';

函数回调($匹配)
{
//预先输入http://如果没有指定协议
$ completeUrl = $ match [1]? $ match [0]:http:// {$ match [0]};

return'< a href ='。$ completeUrl。'>'
。 $ match [2]。 $ match [3]。 $ match [4]。 < / A>;


$ text = preg_replace_callback(& \\ $ rexProtocol $ rexDomain $ rexPort $ rexPath $ rexQuery $ rexFragment(?= [?。!,;:\ ]?(\s | $))&,
'callback',htmlspecialchars($ text));






您可以将自己的想法写入我的答案。 hr>

我正在写...


I am a newbie. I was wondering how to do that, because i've always wanted to make a joke with my friend. Would you please answer? Thanks!

解决方案

You may wonder how it works. I'll try to explain how it should be done by various methods. We'll start first with how regex works and how it is used.


Regex - Regular expression

In computing, a regular expression (abbreviated regex or regexp) is a sequence of characters that forms a search pattern, mainly for use in pattern matching with strings, or string matching, i.e. "find and replace"-like operations.

Basic Syntax

To use regular expressions first you need to learn the syntax. This syntax consists of a series of letters, numbers, dots, hyphens and special signs, which we can group together using different parentheses.

^               The circumflex symbol matches the beginning of the input string or line, although in some cases it can be omitted
$               Same as with the circumflex symbol, the dollar sign matches the end of the input string or line
.               The period matches any single character
?               It will match the preceding pattern zero or one times
+               It will match the preceding pattern one or more times
*               It will match the preceding pattern zero or more times
|               Boolean OR
-               Used when describing a range of elements
()              Groups pattern elements together
[]              Matches any single character between the square brackets
{min, max}      Used to match exact character counts, where min and max are integers
\d              Matches any single digit
\D              Matches any single non digit caharcter
\w              Matches any alpha numeric character including underscore (_)
\W              Matches any non alpha numeric character excluding the underscore character
\s              Matches any single whitespace character

Brackets

Brackets [] have a special meaning when used in the context of regular expressions. They are used to find a range of characters.

[0-9]           Matches any decimal digit from 0 through 9.
[a-z]           Matches any character from lowercase a through lowercase z.
[A-Z]           Matches any character from uppercase A through uppercase Z.
[a-Z]           Matches any character from lowercase a through uppercase Z.

Examples

Let's look at how to use properly the operators. We will do this with an example of the word hello.

/hello/       Matches the word hello
/^hello/      Matches hello at the start of a string. Possible matches are hello or helloworld, but not worldhello
/hello$/      Matches hello at the end of a string or line.
/he.o/        Matches any character between he and o. Possible matches are helo or heyo, but not hello
/he?llo/      Matches either hllo or hello
/hello+/      Matches hello one or more times. E.g. matches hello or hellohello
/he*llo/      Matches llo, hello or hehello, but not hellooo
/hello|world/ Matches either hello or world
/(A-Z)/       Using the hyphen character to denote a range, matches every uppercase character from A to Z. E.g. A, B, C…
/[abc]/       Matches any single character a, b or c
/abc{1}/      Matches precisely one c character after the characters ab. E.g. matches abc, but not abcc
/abc{1,}/     Matches one or more c character after the characters ab. E.g. matches abc or abcc
/abc{2,4}/    Matches between two and four c character after the characters ab. E.g. matches abcc, abccc or abcccc, but not abc

The most common

[^a-zA-Z]       Matches any string not containing any of the characters ranging from a through z and A through Z.
p.p             Matches any string containing p, followed by any character, in turn followed by another p.
^.{2}$          Matches any string containing exactly two characters.
<b>(.*)</b>     Matches any string enclosed within <b> and </b>.
p(hp)*          Matches any string containing a p followed by zero or more instances of the sequence hp.


Regex to match a URL

At first let's look how a URL is built. We only have a couple of options:

  • http://example.com/
  • https://example.com/
  • ftp://example.com/
  • www.example.com
  • user@example.com
  • 127.0.0.1
  • http://example.com:8080/

http://, https://, ftp, www, mail, ip and port.

Method 1 (1/10 points)

// Only mails
$match = preg_match('/[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+/', $string, $array);

Method 2 (5/10 points)

// Without ports, www-s, ip-s and mails
$text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\">\\0</a>", $text);

Method 3 (10/10 points)

/* Proposed by:
 * Søren Løvborg
 * http://stackoverflow.com/users/136796/soren-lovborg
 */

$rexProtocol = '(https?://)?';
$rexDomain   = '((?:[-a-zA-Z0-9]{1,63}\.)+[-a-zA-Z0-9]{2,63}|(?:[0-9]{1,3}\.){3}[0-9]{1,3})';
$rexPort     = '(:[0-9]{1,5})?';
$rexPath     = '(/[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]*?)?';
$rexQuery    = '(\?[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]+?)?';
$rexFragment = '(#[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]+?)?';

function callback($match)
{
    // Prepend http:// if no protocol specified
    $completeUrl = $match[1] ? $match[0] : "http://{$match[0]}";

    return '<a href="' . $completeUrl . '">'
        . $match[2] . $match[3] . $match[4] . '</a>';
}

$text = preg_replace_callback("&\\b$rexProtocol$rexDomain$rexPort$rexPath$rexQuery$rexFragment(?=[?.!,;:\"]?(\s|$))&",
'callback', htmlspecialchars($text));


You can write your own ideas to my answer.


I am writing...

这篇关于使用PHP将纯文本URL转换为活动链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆