[PHP] テキスト内のURLを自動的にリンクにする

2006年10月29日 日曜日 01時10分
E-Mail This Post/Page Print This Post/Page

例えば、沢山URLが含まれるデータをWEB上にアップするとき、そのままではページを開いたユーザーがそのURLを開こうとすると一度コピーしてブラウザのアドレスにペーストしなければならない。もちろん、Sleipnirなどの優れたタブブラウザでは右クリックから一発でURLを開くことも可能だ。

しかし、誰もがそういったソフトを導入しているわけではないので、閲覧した人がワンクリックでURLを開けるようにしておいて何も損はないだろう。

Wordpressではこれを可能にするプラグインがある。 Scott Reilly氏の書いたAuto-hyperlink URLs がそのひとつだ。このスクリプトは一般用には作られていないが、どうすればこれをWordPres以外の他の用途に応用させることができるか、少し分解してみた。

HTML書式などでできた、URLが沢山入っている変数 $text を以下のコードでハイパーリンクに書き換えることができる。

$text = ' ' . $text . ' ';
$text = preg_replace("#([\s{}\(\)\[\]])(([a-z]+?)://([A-Za-z_0-9\-]+\.([^\s{}\(\)\[\]]+[^\s,\.\;{}\(\)\[\]])))#ie",
  "'$1<a href=\"$2\" title=\"$2\">$3://' .  '$4' . '</a>'", $text);
echo $text;

この記事に関連する NEWS

この記事に関連するトピック

このエントリーへの Yahoogle Suggestions

Trackback this Post |

 

記事ヒット TOP 10

IT 関連 NEWS

    There is no entries available for this category.

PHP関連 NEWS

    There is no entries available for this category.

PHP Tips from Social Bookmarks

    There is no entries available for this category.

PHPに関するフォーラムトピック

    There is no entries available for this category.

Tag Cloud

admin Admin Drop Menus Admin Memo Admin Panel advanced drop menus array unique blog cron dabeya Dagon Design Sitemap Generator dropcap Edit N Place error Feedburner game google hack html HTML CSS Internet NEWS Permalink Redirect PHP phpBB2 plugin RSS scripts search stattraq template the content Ultimate Tag Warrior Web Management Windows XP Wordpress WP Plugins WPページ  拡張子 カスタマイズ カスタム関数 カテゴリー コード セキュリティ タグ トップページ トラックバック フィード 投稿 抜粋 日本語 時刻 時間 正規表現 表示 記事

サイト メニュー

Blogtimes image
ぱそずき :D)‐く © 2008
Close
E-mail It