将文本转换为链接-php正则表达式问题

将文本转换为链接-php正则表达式问题,php,regex,url,Php,Regex,Url,我在将纯文本转换为url时遇到了一些问题。 我喜欢的是,如果我有这样的文本:www.google.com,它会转换成 <a href="www.google.com" target="_blank">www.google.com</a> 我有点像RegEx noob,但我试过: $description = preg_replace('@(www.([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@',

我在将纯文本转换为url时遇到了一些问题。 我喜欢的是,如果我有这样的文本:www.google.com,它会转换成

<a href="www.google.com" target="_blank">www.google.com</a>

我有点像RegEx noob,但我试过:

$description = preg_replace('@(www.([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '<a href="$1" target="_blank">$1</a>', $description);
$description=preg_replace('@(www.([-\w\.]+[-\w])+(:\d+)(/([\w/\.-]*(\?\S+?[^\.\S]))?)@,'.$description);
description变量是一段文本,可以包含未转换的url

通过上面的代码,我得到以下链接:

<a target="_blank">www.google.com</a>
www.google.com
因此,href部分被忽略了。这对你们这些RegEx巫师来说一定是小菜一碟,所以提前感谢你们的帮助

如果有其他(更好的?)方法将纯文本转换为url,您可以这样说,我会试试。

在现代浏览器中无法正常工作,因为href值只会附加到当前页面url,例如
http://example.com/www.example.com
。您需要指定协议,例如http/https等

以下内容将替换以ftp、http、https开头的所有文本“链接”,以及带有html a标记的文件

<?php

    $pattern = '/(www|ftp|http|https|file)(:\/\/)?[\S]+(\b|$)/i';
    $string = 'hello http://example.com https://graph.facebook.com    http://www.example.com www.google.com';

    function create_a_tags( $matches ){

        $url = $matches[0];
        if ( 'www' == $matches[1] ){
            $url = 'http://' . $matches[0];
        }
        $escaped = htmlspecialchars($matches[0]);
        return sprintf( '<a href="%s">%s</a>', $url, $escaped );
    }

    echo preg_replace_callback( $pattern, 'create_a_tags', $string );

?>

印刷品

hello <a href="http://example.com">http://example.com</a>
<a href="https://graph.facebook.com">https://graph.facebook.com</a>
<a href="http://www.example.com">http://www.example.com</a>
<a href="http://www.google.com">www.google.com</a>
你好
在现代浏览器中无法正常工作,因为href值将仅附加到当前页面url,例如
http://example.com/www.example.com
。您需要指定协议,例如http/https等

以下内容将替换以ftp、http、https开头的所有文本“链接”,以及带有html a标记的文件

<?php

    $pattern = '/(www|ftp|http|https|file)(:\/\/)?[\S]+(\b|$)/i';
    $string = 'hello http://example.com https://graph.facebook.com    http://www.example.com www.google.com';

    function create_a_tags( $matches ){

        $url = $matches[0];
        if ( 'www' == $matches[1] ){
            $url = 'http://' . $matches[0];
        }
        $escaped = htmlspecialchars($matches[0]);
        return sprintf( '<a href="%s">%s</a>', $url, $escaped );
    }

    echo preg_replace_callback( $pattern, 'create_a_tags', $string );

?>

印刷品

hello <a href="http://example.com">http://example.com</a>
<a href="https://graph.facebook.com">https://graph.facebook.com</a>
<a href="http://www.example.com">http://www.example.com</a>
<a href="http://www.google.com">www.google.com</a>
你好
如果您唯一的问题是链接错误地指向
www.google.com
而不是完全限定的URL,例如
http://www.google.com
,则正确的替换方法是:

$description = preg_replace('@(www.([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '<a href="http://$1" target="_blank">$1</a>', $description);
$description=preg_replace('@(www.([-\w\.]+[-\w])+(:\d+)(/([\w/\.-]*(\?\S+?[^\.\S]))?)@,'.$description);

如果您唯一的问题是链接错误地指向
www.google.com
而不是完全限定的URL,例如
http://www.google.com
,则正确的替换方法是:

$description = preg_replace('@(www.([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '<a href="http://$1" target="_blank">$1</a>', $description);
$description=preg_replace('@(www.([-\w\.]+[-\w])+(:\d+)(/([\w/\.-]*(\?\S+?[^\.\S]))?)@,'.$description);

不久前,我们比较了URL验证和标识的不同方法。请参见正则表达式的说明

我建议你放弃正则表达式,改用正则表达式。(PHP 5.3)解决方案可能类似于:

<?php

$string = 'hello 
http://example.com 
https://graph.facebook.com 
http://www.example.com
www.google.com
ftp://example.com';

$string = preg_replace_callback('#(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))#iS', function($m) {
    // use http as default protocol, if none given
    if (strpos($m[0], '://') === false) {
        $m[0] = 'http://' . $m[0];
    }
    // text -> html is a context switch, take care of special characters
    $_m = htmlspecialchars($m[0]);
    return '<a href="' . $_m . '" target="_blank">' . $_m . '</a>';
}, $string);

echo $string, "\n";

很久以前,我们比较了URL验证和标识的不同方法。请参见正则表达式的说明

我建议你放弃正则表达式,改用正则表达式。(PHP 5.3)解决方案可能类似于:

<?php

$string = 'hello 
http://example.com 
https://graph.facebook.com 
http://www.example.com
www.google.com
ftp://example.com';

$string = preg_replace_callback('#(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))#iS', function($m) {
    // use http as default protocol, if none given
    if (strpos($m[0], '://') === false) {
        $m[0] = 'http://' . $m[0];
    }
    // text -> html is a context switch, take care of special characters
    $_m = htmlspecialchars($m[0]);
    return '<a href="' . $_m . '" target="_blank">' . $_m . '</a>';
}, $string);

echo $string, "\n";

我找到了解决办法。它确实与正则表达式没有任何关系,这是正确的。我的同事在头部添加了这行jquery代码:

$("a").removeAttr('href');
很明显,href属性被删除了。我没有看这个,因为我确信这是一个php/regex问题。删除此选项修复了问题


我意识到这是一个愚蠢的错误,你不可能解决这个问题,所以感谢大家的帮助,+1感谢你们。

我找到了解决方案。它确实与正则表达式没有任何关系,这是正确的。我的同事在头部添加了这行jquery代码:

$("a").removeAttr('href');
很明显,href属性被删除了。我没有看这个,因为我确信这是一个php/regex问题。删除此选项修复了问题



我意识到这是一个愚蠢的错误,您不可能解决这个问题,所以感谢大家的帮助,+1感谢大家。

我已经尝试运行了您的代码,它确实非常有效。您使用的是哪一个php版本?能否为
$description
发布一个示例值?给您:En je bent overal welkom als je maar breedkend bent!“节奏(www.temponieuwsbrief.be)mocht op kotbezoek!要么你在PHP中发现了一个bug,要么你没有正确调试。该文本在PHP 5.3.3、5.3.6和5.3.10中确实有效。在其文件中运行的内容,并告诉我是否有效。我不是PHP爱好者,但我看不出这可能是regex问题。你的替换字符串是静态的,其中包含href,那么regex如何删除它?必须是下游。我试过运行你的代码,它确实工作得很好。你使用的是哪个php版本?你能为
$description
发布一个示例值吗?给你:En je bent overal welkom als je maar breedkend bent!“Tempo(www.temponieuwsbrief.be)mocht op kotbezoek!要么你在PHP中发现了一个bug,要么你没有正确调试。该文本在PHP5.3.3、5.3.6和5.3.10中确实有效。在它的文件上运行的内容,并让我知道这是否有效。我不是一个PHP的家伙,但我不明白这怎么可能是正则表达式的问题。您的替换字符串是静态的,其中包含href,那么regex如何删除它呢?必须是下游。但是如果文本是这样的:www.google.com,我想得到这样的文本:google.com“>?我已经编辑了上面的代码来处理www URL(通过将http://添加到href属性),但是现在可能会产生一些误报(我还没有测试过它)但是如果文本是这样的:www.google.com,我想得到这样的文本:google.com“>?我已经编辑了上面的代码来处理www URL(通过将http://添加到href属性),但是现在可能会产生一些误报(我还没有测试它)他目前使用的正则表达式没有任何根本性的错误-生成的标记看起来无效(不是href上的scheme)。我从未说过他的正则表达式有任何错误。我刚才解释说有个更好的。此外,此解决方案是唯一一个清理URL以用于HTML的解决方案。我确实认为有一件事很重要。如果你只想回答核心问题,而不想看大局——做我的客人,投你想要的反对票……它编译得不好,我会遇到以下错误:解析错误:语法错误,意外的T_常量