-1

我如何打开一些这样的链接

http://someaddr.ess/linked [URL NAME]

进入

<a href="http://someaddr.ess/linked">URL NAME</a>

我正在寻找 PHP 或 jQuery 的解决方案。

*我在 stackoverflow 中没有找到专门针对此问题的解决方案。如果您已经知道一些答案,请链接到它。

4

3 回答 3

4

试试这个代码:

$a =  'http://someaddr.ess/linked [URL NAME]';
echo preg_replace('~(https?://\S+)\s*\[([^\]]+)\]~', '<a href="$1">$2</a>', $a);
//=> <a href="http://someaddr.ess/linked">URL NAME</a>
于 2013-11-06T07:32:08.917 回答
1

你可以试试这个,

<?php
    $input ="http://someaddr.ess/linked [URL NAME]";
    $input =str_replace("]", "", $input);
    list($url, $URLTEXT) =explode("[", $input);

    $URL = preg_replace('!(http|ftp|scp)(s)?:\/\/[a-zA-Z0-9.?&_/]+!', "<a href=\"\\0\">".$URLTEXT."</a>",$url);     

    echo $URL;
 ?>
于 2013-11-06T07:34:33.427 回答
0

试试喜欢

$str = "http://someaddr.ess/linked [URL NAME]";
$new_arr = explode(' ',$str);

preg_match_all('/\[([A-Za-z]+?)\]/', $str, $text);
echo "<a href='".$new_arr[0]."'>".$text[0]."</a>";
于 2013-11-06T07:29:44.513 回答