4

質問のコメントに長いURLをコメントするときに、StackOverflowが作成するのと同じように、PHPで短いURLを作成するためのヘルプが必要です。

StackOverflowhttp://www.noupe.com/how-tos/10-ways-to-automatically-manually-backup-mysql-database.htmlは、このように長いURLを短いURLに短縮しますnoupe.com/...

私のアプリケーションには、同様の種類の機能が必要です。誰かがPHPでそれを行う方法をアイデアやコードで教えてもらえますか?

StackOverflowで検索するのに疲れましたが、質問は見つかりませんでした。SOでそのようなタイプの質問を見たのを覚えていますが、今はそれを見つけることができません:(

助けてください!

4

5 に答える 5

7

単純なアルゴリズムの概要です。

  1. リンクの長さがX文字を超えていないかどうかを確認してください。
  2. http://またはhttps://を最初に。で削除しstr_replaceます。
  3. で分解し/、返された配列の最初の項目のみを保持します。
  4. 手順3で複数のアイテムが見つかった場合/...は、最後に追加します。
  5. オプション。www.で始まるを削除しstr_replaceます。

この見つかった文字列を使用して名前を付け[shortURL]、アンカーを作成します。

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

<a>私の推測では、ソース出力でタグを検索し、それに応じて値を変更する必要があります。href同じままですが、リンク名を必要な名前に変更します。

しかし、それは1つのアイデアにすぎません...いつでも新しいものを試すことができます。

外出先でJavaScriptを使用してこれを実現する方法もあるはずです。

箱から出して考えてください!

于 2010-10-19T08:27:56.847 に答える
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 に答える
1

正規表現を使用してURLを取得できます。ここでは、見つかったURLからタグを作成する方法と、タグ<a>のコンテンツを短縮する方法の2つの例を示します。<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 に答える
-3

一致するファイルがない「カスタム」URLを作成するには、Webサーバーを構成する必要があります。Apacheを使用していて、そうする権利がある場合は、 http mod_rewrite//httpd.apache.org/docs/1.3/mod/mod_rewrite.htmlを参照してください。

そしてチュートリアル: http ://articles.sitepoint.com/article/guide-url-rewriting

于 2010-10-19T08:27:55.210 に答える