不二云端 - PHP http://www.2sv.cn/tag/PHP/ zh-CN Tue, 02 Sep 2025 23:28:06 +0800 Tue, 02 Sep 2025 23:28:06 +0800 WordPress代码实现彩色标签云 http://www.2sv.cn/archives/281/ http://www.2sv.cn/archives/281/ Tue, 02 Sep 2025 23:28:06 +0800 Eleven AI摘要:这段代码通过修改WordPress博客的`functions.php`文件,实现了彩色标签云功能。它使用了PHP中的正则表达式来为每个标签生成随机背景色,并通过CSS为标签添加圆角和过渡效果。最终,标签显示为具有不同颜色背景的圆角标签。

Powered by AISummary.

要在WordPress博客中实现彩色标签云,您可以使用一些HTML、CSS和PHP代码。以下是一个简单的步骤来实现这个功能,方法有很多这个我认为是最简单的一种。

在当前主题目录下面的functions.php里面加入以下代码:

//圆角背景色标签  
function colorCloud($text) {  
$text = preg_replace_callback('|<a (.+?)>|i', 'colorCloudCallback', $text);  
return $text;  
}  
function colorCloudCallback($matches) {  
$text = $matches[1];  
$colors = array('F99','C9C','F96','6CC','6C9','37A7FF','B0D686','E6CC6E');  
$color=$colors[dechex(rand(0,7))]; 
$pattern = '/style=(\'|\")(.*)(\'|\")/i';  
$text = preg_replace($pattern, "style=\"display: inline-block; *display: inline; *zoom: 1; color: #fff; padding: 1px 5px; margin: 0 5px 5px 0; background-color: #{$color}; border-radius: 3px; -webkit-transition: background-color .4s linear; -moz-transition: background-color .4s linear; transition: background-color .4s linear;\"", $text);  
$pattern = '/style=(\'|\")(.*)(\'|\")/i';  
return "<a $text>";  
}  
add_filter('wp_tag_cloud', 'colorCloud', 1);
]]>
0 http://www.2sv.cn/archives/281/#comments http://www.2sv.cn/feed/tag/PHP/
PHP 实现“万能”的短网址还原 http://www.2sv.cn/archives/215/ http://www.2sv.cn/archives/215/ Wed, 02 Jul 2025 16:55:12 +0800 Eleven AI摘要:该文章介绍了如何使用PHP实现一个“万能”的短网址还原功能。通过使用curl_getinfo获取短网址的重定向头信息,进而提取原始网址。提供了具体的PHP函数实现代码,并测试了多个短网址的还原效果。不过,无法处理通过JavaScript跳转的短网址。

Powered by AISummary.

常见的短网址都是通过 301 或 302 跳转的方式实现重定向到目标网站的,因此我们可以使用 PHP 的 curl_getinfo 来取得 header 中的重定向地址,也就是短网址对应的原始网址(嗯,原理就是这么简单……

完整的函数代码如下:

/***
 * 万能短网址还原函数
 * @param $shortUrl 短网址
 * @return 原始网址 | 空(还原失败或非短网址)
 */
function restoreUrl($shortUrl) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $shortUrl);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0');
    curl_setopt($curl, CURLOPT_HEADER, true);
    curl_setopt($curl, CURLOPT_NOBODY, false);
    curl_setopt($curl, CURLOPT_TIMEOUT, 15);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
    $data = curl_exec($curl);
    $curlInfo = curl_getinfo($curl);
    curl_close($curl);
    if($curlInfo['http_code'] == 301 || $curlInfo['http_code'] == 302) {
        return $curlInfo['redirect_url'];
    }
    return '';
}

使用方法:

$shortUrl = 'https://url.cn/54VbB8h';    // 要还原的短网址
$orinalUrl = restoreUrl($shortUrl);
if($orinalUrl) {
    echo "短网址 {$shortUrl} 的还原结果:{$orinalUrl}";
} else {
    echo "短网址还原失败";
}

经过实测,该函数可以顺利实现下列短网址的还原:
https://url.cn/54VbB8h
http://t.cn/AiR8Qoyp
http://uee.me/cAhq8
http://rrd.me/eWCg3
https://sohu.gg/MSYnnHo02
https://dwz.cn/I5l2YWKL
http://1t.click/bceu
http://dwz.win/qMp
http://qq.cn.hn/e8N
不过,诸如 https://m.tb.cn/h.ew5NAEA 这种在前端使用 js 跳转的短网址就无能为力了

]]>
0 http://www.2sv.cn/archives/215/#comments http://www.2sv.cn/feed/tag/PHP/