【问题标题】:PHP Regex to make URL's clickable links AND extract link title from custom inputPHP Regex 制作 URL 的可点击链接并从自定义输入中提取链接标题
【发布时间】:2012-10-24 10:26:37
【问题描述】:

我有一个自定义编辑用户界面,允许用户输入他们自己的 URL,到目前为止,我有正则表达式来查找 URL 并将它们全部转换为可点击的 html 链接。但我也想给用户输入他们自己的链接标题的选项,类似于 StackOverflow 上的格式:

[链接名称](http://www.yourlink.com/)

我将如何更改下面的代码以从括号中提取标题、从括号中提取 URL,并将常规 URL 转换为可点击链接(即使他们只是输入 http://www.yourlink.com/ 而没有标题)?

$text = preg_replace('/(((f|ht){1}tp:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i',
                       '<a href="\\1" target="_blank">\\1</a>', $text);
$text = preg_replace('/([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i',
                       '\\1<a href="http://\\2" target="_blank">\\2</a>', $text);
$text = preg_replace('/([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})/i',
                       '<a href="mailto:\\1">\\1</a>', $text);

【问题讨论】:

    标签: php regex url hyperlink


    【解决方案1】:

    首先你必须处理这些带有描述的链接,像这样:

    $text = preg_replace(
        '/\[([^\]]+)\]\((((f|ht){1}tp:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)\)/i',
        '<a href="\\2" target="_blank">\\1</a>', 
        $text
    );
    

    但是现在,放置在 href 中的常规 URL 将在常规链接的下一次替换迭代中匹配,因此我们需要对其进行修改以排除它,例如仅当它前面没有 " 时才匹配:

    $text = preg_replace(
        '/(^|[^"])(((f|ht){1}tp:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/i',
        '\\1<a href="\\2" target="_blank">\\2</a>', 
        $text
    );
    

    【讨论】:

    • 非常感谢您的回复。不幸的是,您提供的第二个示例不适用于任何链接。您提供的第一个示例适用于带有您所说的描述的链接,但不是没有描述的链接。
    • 这不是一个例子,它是对您的第一个替换模式的改进。您仍然需要按此顺序运行所有这些以涵盖所有所需的模式。
    • 我认为可能是这种情况,所以我运行了所有这些,但没有描述的 URL 仍然没有被制作成链接。那些有描述的人。
    • 我知道出了什么问题...如果您有一个没有描述作为第一个值的链接,它似乎不适用于该链接。所有其他链接都很好。不知道如何解决这个问题..有什么想法吗? codepad.viper-7.com/QMsbW5
    • 好的,通过将非撇号字符设为可选来修复
    【解决方案2】:

    试试这个:

    <?php
    $text = "hello http://example.com sample
    [Name of Link](http://www.yourlink.com/)
    [Name of a](http://www.world.com/)
    [Name of Link](http://www.hello.com/)
    <a href=\"http://stackoverflow.com\">hello world</a>
    <a href='http://php.net'>php</a>
    ";
    echo nl2br(make_clickable($text));
    function make_clickable($text) {
       $text = preg_replace_callback(
        '#\[(.+)\]\((\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|)/))\)#', 
        create_function(
          '$matches',
          'return "<a href=\'{$matches[2]}\'>{$matches[1]}</a>";'
        ),
        $text
      );
      $text = preg_replace_callback('#(?<!href\=[\'"])(https?|ftp|file)://[-A-Za-z0-9+&@\#/%()?=~_|$!:,.;]*[-A-Za-z0-9+&@\#/%()=~_|$]#', create_function(
          '$matches',
          'return "<a href=\'{$matches[0]}\'>{$matches[0]}</a>";'
        ), $text);
      return $text;
    }
    

    基于以下链接编写(编辑):

    Best way to make links clickable in block of text

    Make links clickable with regex

    和...

    【讨论】:

    • 您好,感谢您的回复。您的示例适用于没有标题的链接,但似乎不适用于标题格式为 [链接名称](http://www.yourlink.com/) 的链接。
    • 嗨,您好,但它有效:-?运行示例,它应该可以工作:-?
    • 你是对的,当我再次运行它时它起作用了,我很抱歉。谢谢!
    猜你喜欢
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-03
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 2016-01-25
    相关资源
    最近更新 更多