1 回答

TA貢獻1862條經驗 獲得超7個贊
您的變量$link
不是數組,因此它只需要最后分配的值$link
.?您可以preg_replace
用str_replace
匹配項和鏈接替換并傳遞數組。
您也可以使用preg_replace_callback()
并且可以將 $matches 直接傳遞給將替換為鏈接的函數。
function link_isgd($text)
{
? ? $regex = '@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-~]*(\?\S+)?)?)?)@';
? ? preg_match_all($regex, $text, $matches);
? ? $links = [];
? ? foreach ($matches[0] as $longurl) {
? ? ? ? $tiny = file_get_contents('http://isnot.gd/api.php?longurl=' . $longurl . '&format=json');
? ? ? ? $json = json_decode($tiny, true);
? ? ? ? foreach ($json as $key => $value) {
? ? ? ? ? ? if ($key == 'errorcode') {
? ? ? ? ? ? ? ? $links[] = $longurl;
? ? ? ? ? ? } else if ($key == 'shorturl') {
? ? ? ? ? ? ? ? $links[] = $value;
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? $links = array_map(function ($el) {
? ? ? ? return '<a href="' . $el . '" target="_blank">' . $el . '</a>';
? ? }, $links);
? ? return str_replace($matches[0], $links, $text);
}
- 1 回答
- 0 關注
- 157 瀏覽
添加回答
舉報