2010-10-19 92 views
4

我需要一些PHP帮助来创建短url,就像StackOverflow在评论某个问题时评论任何长URL一样。缩短链接上的URL文本,如stackoverflow?

StackOverflow上缩短http://www.noupe.com/how-tos/10-ways-to-automatically-manually-backup-mysql-database.html长的URL短网址这样noupe.com/...

我需要在我的应用程序类似这样的功能。任何人都可以提供一些想法或代码如何在PHP中做到这一点?

我累了在StackOverflow上搜索它,但没有发现任何问题。我记得我曾见过这种类型的问题上如此,但现在我不能找到它:(

请帮帮忙!

回答

7

只是简单算法的轮廓。

  1. 查看链接的长度是否超过了X个字符。
  2. 删除http://https://开头str_replace
  3. 爆炸在/并只保留返回的数组中的第一个项目。
  4. 如果在步骤3中发现超过1个项目,请在末尾添加/...
  5. 可选。在开始时删除www.str_replace

有了这个发现字符串,并将其命名为[shortURL],您撰写你的锚:

<a href="[fullURL]">[shortURL]</a> 
+0

虽然这是真的,你没有解决这个事实,该URI也必须在用户的输入中找到。 – 2010-10-19 18:22:48

+0

我不认为URL的来源与问题相关。 – 2010-10-19 18:39:27

-3

要创建“定制”的URL没有匹配的文件,你“将不得不配置你的服务器如果你使用Apache,并有权利这样做,你可以看看mod_rewritehttp://httpd.apache.org/docs/1.3/mod/mod_rewrite.html

和教程: http://articles.sitepoint.com/article/guide-url-rewriting

+8

你完全不在话题:)。 – 2010-10-19 08:29:47

+2

有没有试过阅读一个问题的身体呢? – 2010-10-19 08:30:40

+0

您的答案不正确的原因是显示在锚标记中的文本不必与锚标记的href属性指向的URI相匹配。也就是说,你无疑是正确的,几乎所有其他的在stackoverflow.com上的URI都是使用网址重写技术创建的,比如在你发布的链接中描述的技术。 – 2010-10-19 18:28:43

2

我的猜测是,你只需要在哟搜索<a>标签你的源输出,并相应地改变它的值。 href保持不变,但您将链接名称更改为所需。

但这只是一个想法...你总是可以尝试新的东西。

还应该有一种方法来完成这与JavaScript在这去。

开箱即用!

1

这里是一个链接来替换URL的功能。你只需要调整你想要的格式。也许使用parse_url()

<?php 
function URLref($sentence){ 
    $temp = explode(" ", $sentence); 
    $new = ""; 
    foreach($temp as $i){ 
    if(preg_match('([A-Za-z][A-Za-z0-9+.-]{1,120}:[A-Za-z0-9/](([A-Za-z0-9$_.+!*,;/?:@&~=-])|%[A-Fa-f0-9]{2}){1,333}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*,;/?:@&~=%-]{0,1000}))?)', $i)){ 
     $new .= '<a href="'.$i.'">'.$i.'</a>'; 
    }else{ 
     $new .= "$i "; 
    } 
    } 
    return trim($new); 
} 
$sentence = "My site ULR is http://www.google.com/lolz.html"; 

echo URLref($sentence); 
+0

嗨,我喜欢你的答案,如果我们想缩短网址,就像OP说的那样。如果当你回显$句子,它会给**'我的网站ULR是http://www.google.com/...'**?怎么做 ? – 2016-08-05 08:26:02

+0

亲爱的@petah我想我修好了,我有一个问题,如果网址没有http?它只有**'www.google.com' **你怎么能修改你的正则表达式来找到这些URL没有http://,谢谢 – 2016-08-05 13:36:29

1

你可以使用正则表达式,这里就如何创建一个从发现的网址<a>标签两个例子,并就如何缩短<a>标签内容检索网址:

<?php 

$orig_text = <<<TEXT 
This is some text. http://www.example.com/this-is-a-quite-long-url-to-be-shortened.html 
http://www.example.com/another-url-to-be-shortened and http://www.example.com/another-one-that-is-longer-than-limit then 
http://www.example.com/an-ok-url and some text to finish the sentence. 

Now, try with an HTTPS url: https://www.example.com/this-https-url-is-too-long. 

And with an already-created tag <a href='http://www2.example.com/this-is-another-long-url.html'>http://www2.example.com/this-is-another-long-url.html</a> <a href='http://www2.example.com/my-test-url-goes-here.html'>And this is just some long long link description to be shortened</a>. More text here. 

TEXT; 

$PATTERN_URL='#(?:href=[\'"]?)?!(https?://([^/]+)/([^\s]+))\b#'; 
define('URL_LENGTH_LIMIT', 36); 

function create_a_tag($matches) { 
    $url = $matches[1]; 
    $label = $matches[1]; 
    if (strlen($label) > URL_LENGTH_LIMIT) $label = $matches[2] . '/...'; 
    return "<a href='$url'>$label</a>"; 
} 

function shorten_url_or_text($url) { 
    if (strlen($url) > URL_LENGTH_LIMIT) { 
    $matches = array(); 
    if (preg_match('#^(https?://[^/]*).*#', $url, $matches)) { 
     // Shorten as for URLS 
     return $matches[1] . '/...'; 
    } 
    else { 
     // Trim to a given length 
     return substr($url, 0, URL_LENGTH_LIMIT-3) . '...'; 
    } 
    } 
    else { 
    return $url; 
    } 
} 

function shorten_a_text($matches) { 
    $text = shorten_url_or_text($matches[2]); 
    return $matches[1] . $text . $matches[3]; 
} 

// This will replace urls with their shortened form 
echo "----- CREATE <A> TAGS -----\n"; 
$text2 = preg_replace_callback($PATTERN_URL, 'create_a_tag', $orig_text); 
echo $text2 . "\n"; 

// This will shorten content inside <a> tags 
echo "----- CREATE <A> TAGS -----\n"; 
$text3 = preg_replace_callback('@(<a[^>]*>)([^<]*)(</a>)@i', 'shorten_a_text', $text2); 
echo $text3; 
echo "\n"; 
+0

看起来像stackoverflow没有猜测正确的语法高亮..和heredoc字符串没有正确着色。 – redShadow 2010-10-19 09:19:16