Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.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_Html - Fatal编程技术网

Php 将特定字符串转换为链接?

Php 将特定字符串转换为链接?,php,html,Php,Html,我正在寻找一种php方法来搜索文本字符串,并将该字符串中的文本转换为链接 n being numbers 1-9 Jnn:nn:nn:nn 链接位看起来像这样 <a href='http://juno.astroempires.com/map.aspx?loc=Jnn:nn:nn:nn'>Jnn:nn:nn:nn</a> 希望这是有意义的。我知道这是可能的,但我不知道怎么做 这是我提出的最终解决方案,因为我集成到一个现有函数中,感谢bumperbox和edit

我正在寻找一种php方法来搜索文本字符串,并将该字符串中的文本转换为链接

n being numbers 1-9
Jnn:nn:nn:nn
链接位看起来像这样

<a href='http://juno.astroempires.com/map.aspx?loc=Jnn:nn:nn:nn'>Jnn:nn:nn:nn</a>

希望这是有意义的。我知道这是可能的,但我不知道怎么做


这是我提出的最终解决方案,因为我集成到一个现有函数中,感谢bumperbox和edit Tobiask的想法

function makeClickableLinks($text){
    $text = html_entity_decode($text);
    $text = " ".$text;
    $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
            '<a href="\\1" target=_blank>\\1</a>', $text);
    $text = eregi_replace('(((f|ht){1}tps://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
            '<a href="\\1" target=_blank>\\1</a>', $text);
    $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
            '\\1<a href="http://\\2" target=_blank>\\2</a>', $text);
    $text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})',
            '<a href="mailto:\\1" target=_blank>\\1</a>', $text);
    $text = eregi_replace('(J[0-9]{2}):([0-9]{2}):([0-9]{2}):([0-9]{2})',
            '<a href="http://juno.astroempires.com/map.aspx?loc=\\1:\\2:\\3:\\4" target=_blank>\\1:\\2:\\3:\\4</a>', $text);
    return $text;
}
函数makeClickableLinks($text){
$text=html\u实体\u解码($text);
$text=”“.$text;
$text=eregi_replace(“((f|ht{1}tp://)[-a-zA-Z0-9@:%\+.~#?&/=]+),
“$文本);
$text=eregi_replace(“((f|ht){1}tps://)[-a-zA-Z0-9@:%\+.~#?&/=]+),
“$文本);
$text=eregi_replace(“([[:space:]()[{}])(www.[-a-zA-Z0-9@:%\+.~#?&/=]+),
“\\1”,$text);
$text=eregi_replace(“([[u\.0-9a-z-]+@[0-9a-z][0-9a-z-]+\)+[a-z]{2,3}”),
“$文本);
$text=eregi_replace('(J[0-9]{2}):([0-9]{2}):([0-9]{2}):([0-9]{2}),
“$文本);
返回$text;
}
这是在ajax/mysql聊天系统中使用的。我确实让jQuery完成了这项工作,但如果我只存储链接,用户就不需要这么做了。

看看preg\u replace

我还没有测试过,但应该很接近

$str = preg_replace("/(J\d{2}:\d{2}:\d{2}:\d{2})/s", "<a href="{$1}">{$1}</a>", $str);
$str=preg_replace(“/(J\d{2}:\d{2}:\d{2}:\d{2})/s“,”,$str);
\d匹配一个数字 {2} 表示两位数

{$1}=第一组()中匹配的所有内容查看preg\u replace

我还没有测试过,但应该很接近

$str = preg_replace("/(J\d{2}:\d{2}:\d{2}:\d{2})/s", "<a href="{$1}">{$1}</a>", $str);
$str=preg_replace(“/(J\d{2}:\d{2}:\d{2}:\d{2})/s“,”,$str);
\d匹配一个数字 {2} 表示两位数


{$1}=使用正则表达式引擎在第一组()中匹配的所有内容都是完全多余的。为什么不试试这样的方法呢:

$url_base   = 'http://juno.astroempires.com/map.aspx';
$loc        = 'Jnn:nn:nn:nn';
$parameters = array('loc' => $loc);

$url = sprintf('<a href="%s">%s</a>', $url_base.'?'.http_build_query($parameters), $loc);

// do whatever you need with $url
echo $url;
$url\u base='1!'http://juno.astroempires.com/map.aspx';
$loc='Jnn:nn:nn';
$parameters=数组('loc'=>$loc);
$url=sprintf('''$url\u base.'?'.http\u build\u query($parameters),$loc);
//使用$url执行任何需要的操作
echo$url;

使用正则表达式引擎完全是过火了。为什么不试试这样的方法呢:

$url_base   = 'http://juno.astroempires.com/map.aspx';
$loc        = 'Jnn:nn:nn:nn';
$parameters = array('loc' => $loc);

$url = sprintf('<a href="%s">%s</a>', $url_base.'?'.http_build_query($parameters), $loc);

// do whatever you need with $url
echo $url;
$url\u base='1!'http://juno.astroempires.com/map.aspx';
$loc='Jnn:nn:nn';
$parameters=数组('loc'=>$loc);
$url=sprintf('''$url\u base.'?'.http\u build\u query($parameters),$loc);
//使用$url执行任何需要的操作
echo$url;

希望这有意义
否。添加一些来自->的示例TO@genesisφ很抱歉刚才回复的太慢,你可以发送任何类似的内容。$string=“words with some other shit www.link.com还有一些愚蠢的东西,比如J50:45:23:23”;所以当你将这些srting发送到makeClickableLinks($string)这样的函数时它将把原文改为:“还有一些其他的狗屎的话,这里有一些愚蠢的东西,比如>我希望这是有意义的创世记
希望这是有意义的
否。添加一些来自->TO@genesisφ很抱歉刚才回的回复太慢,您可以发送任何类似的邮件。$string=”还有一些愚蠢的东西,比如J50:45:23:23“所以当你把这个srting发送到makeClickableLinks($string)这样的函数时,它会把原来的改为:还有一些其他狗屎的话,这里也有一些愚蠢的东西,比如>我希望这是有意义的genesisThanks alot这就是我要找的。非常感谢这就是我要找的。