Php 缩短像stackoverflow这样的链接上的URL文本?
我需要一些PHP帮助来创建短URL,就像StackOverflow在对问题的评论中评论任何长URL时创建的一样 堆栈溢出缩短Php 缩短像stackoverflow这样的链接上的URL文本?,php,url,short,Php,Url,Short,我需要一些PHP帮助来创建短URL,就像StackOverflow在对问题的评论中评论任何长URL时创建的一样 堆栈溢出缩短http://www.noupe.com/how-tos/10-ways-to-automatically-manually-backup-mysql-database.htmllong-url到像这样的短urlnoupe.com/… 我需要在我的应用程序中使用类似的功能。有人能给出一些想法或代码如何在PHP中做到这一点吗 我在StackOverflow上搜索了很久,但没有
http://www.noupe.com/how-tos/10-ways-to-automatically-manually-backup-mysql-database.html
long-url到像这样的短urlnoupe.com/…
我需要在我的应用程序中使用类似的功能。有人能给出一些想法或代码如何在PHP中做到这一点吗
我在StackOverflow上搜索了很久,但没有找到任何问题。我记得我曾见过这样的问题,但现在我找不到:(
请帮助!要创建没有匹配文件的“自定义”URL,您必须配置您的Web服务器。如果您使用的是Apache并且有权这样做,您可以查看mod_rewrite
:
和教程:
我的猜测是,您只需在源输出中搜索
标记,并相应地更改其值。href
保持不变,但您可以将链接名称更改为所需名称
但这只是一个想法…你总是可以尝试新的东西
还应该有一种方法可以在运行javascript时实现这一点
开箱思考!只是一个简单算法的概要
str\u replace
开头的http://
或https://
/
处分解,只保留返回数组中的第一项/…
str\u replace
删除开头的www.
[shortURL]
,您可以组成您的锚:
<a href="[fullURL]">[shortURL]</a>
这是一个用链接替换url的函数。您只需调整格式即可。可以使用
parse\u url()
您可以使用正则表达式检索URL,这里有两个关于如何创建的示例。这里有更多文本。
文本;
$PATTERN_URL='#(?:href=[\'”]?)?!(https?:/([^/]+)/([^\s]+)\b#';
定义(“URL长度限制”,36);
函数创建一个标记($matches){
$url=$matches[1];
$label=$matches[1];
如果(strlen($label)>URL_LENGTH_LIMIT)$label=$matches[2]。/…';
返回“”;
}
函数缩短\u url\u或\u文本($url){
如果(strlen($url)>url\u长度\u限制){
$matches=array();
if(preg#u match('#^(https?:/[^/]*)..#',$url,$matches)){
//缩短URL的长度
返回$matches[1]。/…';
}
否则{
//修剪到给定长度
返回substr($url,0,url_LENGTH_LIMIT-3)。“…”;
}
}
否则{
返回$url;
}
}
函数缩短文本($matches){
$text=缩短url或文本($matches[2]);
返回$matches[1]。$text.$matches[3];
}
//这将用其缩写形式替换URL
echo“----创建标记------\n”;
$text2=preg_replace_回调($PATTERN_URL,'create_a_tag',$orig_text);
回显$text2。“\n”;
//这将缩短标记内的内容
echo“----创建标记------\n”;
$text3=preg_replace_回调('@(]*>)([^缩短的是
标记内的文本,而不是URL,因此您可能想重新表述您的问题。您可以使用简单的字符串操作来实现此目的。没有任何看起来stackoverflow没有猜到正确的语法高亮灯..并且heredoc字符串没有正确着色。虽然这是真的,但您没有找到正确的语法解决用户输入中也必须找到URI的问题。我认为URL的来源与问题无关。您的答案不正确的原因是锚定标记中显示的文本不必与锚定标记的href属性指向的URI匹配。也就是说,几乎所有的URL都是正确的e stackoverflow.com上的其他URI肯定是使用url重写技术创建的,如您发布的链接中所述。我只是做了一个错误的假设,当Alin发布他的评论时,我重新阅读并理解了发生的事情。感谢您花时间解释为什么您投了否决票:)您好,我喜欢您的回答,如果我们想像OP所说的那样缩短url会怎么样?如果您回显$SECTION,它会给出我的站点ULR是什么http://www.google.com/...
?怎么做?亲爱的@petah我想我已经修好了,我有一个问题,如果url没有http怎么办?它只有www.google.com
你怎么能修改它我们的正则表达式也可以找到那些没有http://的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);
<?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";