PHP:通过从中心切割URL缩短URL?

Rya*_*yan 4 php

我在很多论坛上看到他们从中心切出网址并加上3个点,如果它很长,以便缩短它.

示例:ajaxify多部分编码形式(上传表单) ---将---> http://stackoverflow.c...ed-form-upload-forms

如何使用纯PHP做到这一点?

谢谢

Vol*_*erK 10

例如通过preg_replace()

$testdata = array(
  'http://stackoverflow.com/questions/1899537/ab',
  'http://stackoverflow.com/questions/1899537/abc',
  'http://stackoverflow.com/questions/1899537/abcd',
  'http://stackoverflow.com/questions/1899537/ajaxify-multipart-encoded-form-upload-forms'
);

foreach ($testdata as $in ) {
  $out = preg_replace('/(?<=^.{22}).{4,}(?=.{20}$)/', '...', $in);
  echo $out, "\n";
}
Run Code Online (Sandbox Code Playgroud)

版画

http://stackoverflow.com/questions/1899537/ab
http://stackoverflow.c...uestions/1899537/abc
http://stackoverflow.c...estions/1899537/abcd
http://stackoverflow.c...ed-form-upload-forms
Run Code Online (Sandbox Code Playgroud)