Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP字符串中查找链接并将其转换为超链接?_Php - Fatal编程技术网

在PHP字符串中查找链接并将其转换为超链接?

在PHP字符串中查找链接并将其转换为超链接?,php,Php,在PHP字符串中找到一个链接,并将其转换为超链接,使其可以单击并在新选项卡中打开 PHP代码/字符串: 用这个例子来解决你的问题 <?php function makeClickableLink($text) { $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\\1">\\1</a>', $text); $text = ere

在PHP字符串中找到一个链接,并将其转换为超链接,使其可以单击并在新选项卡中打开

PHP代码/字符串:


用这个例子来解决你的问题

<?php

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

// Usage 

// Email address example
$text = "you@example.com";
echo makeClickableLink($text);

// URL example
$text = "http://www.example.com";
echo makeClickableLink($text);  

// FTP URL example
$text = "ftp://ftp.example.com";
echo makeClickableLink($text); 

?>

使用这个库。它会自动从字符串中检测链接并使其可单击。您不需要创建链接,只需添加具有链接的字符串即可。此库将自动检测它


这有用吗?可能的副本你可以参考那太好了它帮助了我。另外,我想问,如果我还想从该链接显示一个拇指尾呢?POSIX正则表达式函数在3年前的PHP5.3中就被弃用了?这个函数几乎在我学习PHP的时候就被弃用了!看见
http://www.test.com
<?php

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

// Usage 

// Email address example
$text = "you@example.com";
echo makeClickableLink($text);

// URL example
$text = "http://www.example.com";
echo makeClickableLink($text);  

// FTP URL example
$text = "ftp://ftp.example.com";
echo makeClickableLink($text); 

?>