4

我需要一些 PHP 帮助来创建短 URL,就像 StackOverflow 在对问题的评论中评论任何长 URL 时创建的一样。

StackOverflow 将http://www.noupe.com/how-tos/10-ways-to-automatically-manually-backup-mysql-database.html长 url 缩短为这样的短 urlnoupe.com/...

我的应用程序需要类似的功能。谁能给出一些想法或代码如何在 PHP 中做到这一点?

我厌倦了在 StackOverflow 上搜索它,但没有找到任何问题。我记得我在 SO 上看到过这样的问题,但现在我找不到它:(

请帮忙!

4

5 回答 5

7

只是一个简单算法的概述。

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

有了这个找到的字符串,命名它[shortURL],你就组成了你的锚:

<a href="[fullURL]">[shortURL]</a>
于 2010-10-19T08:33:18.913 回答
2

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

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

还应该有一种方法可以使用 javascript on-the-go 来实现这一点。

打破常规思考问题!

于 2010-10-19T08:27:56.847 回答
1

您可以使用正则表达式检索 URL,这里有两个示例说明如何<a>从找到的 URL 创建标签以及如何缩短<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";
于 2010-10-19T09:17:23.240 回答
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);
于 2010-10-19T08:46:10.273 回答
-3

要创建没有匹配文件的“自定义”URL,您必须配置您的网络服务器。如果您使用 Apache 并且有权这样做,您可以查看mod_rewritehttp ://httpd.apache.org/docs/1.3/mod/mod_rewrite.html

还有一个教程: http ://articles.sitepoint.com/article/guide-url-rewriting

于 2010-10-19T08:27:55.210 回答